1

UserProfile と Inbox の 2 つのエンティティがあります。

これは私のコードですUserProfile

public partial class UserProfile {
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    //--------------------------
    public virtual ICollection<Messages.Inbox> Inboxes { get; set; }
}

public partial class UserProfile {
    public static void Configure(DbModelBuilder mb) {
        mb.Entity<UserProfile>().HasKey(up => up.Id);
    }
}

そして、これは私のコードですInbox:

public partial class Inbox {
    public int Id { get; set; }
    public int UserId { get; set; }//FK to userProfile
    public Accounts.UserProfile User { get; set; }
    public DateTime MessageDateTime { get; set; }
    public string Context { get; set; }
    public int SenderId { get; set; }//FK to userProfile
    public Accounts.UserProfile Sender { get; set; }
}

public partial class Inbox {
    public static void Configure(DbModelBuilder mb) {
        mb.Entity<Inbox>().HasKey(up => up.Id);
        mb.Entity<Accounts.UserProfile>().HasMany(up => up.Inboxes)
            .WithRequired(p => p.User)
            .HasForeignKey(p => p.UserId);
        mb.Entity<Accounts.UserProfile>().HasMany(up => up.Inboxes)
            .WithRequired(p => p.Sender)
            .HasForeignKey(p => p.SenderId);
    }
}

と の間のこれら 2 つの関係をどのように管理できInboxますUserProfileか?

4

1 に答える 1

0

受信トレイを追加するためにユーザー/UI に公開する DTO がある場合、その dto にはユーザー/送信者のプロパティを含めず、ID の SenderId/UserId のみを含めてください。受信トレイ エンティティからのユーザー/送信者。したがって、UserProfile テーブルと結合する代わりに、エンティティ フレームワークを使用してそれを含めることができます。それが役立つことを願っています。

于 2014-10-18T23:35:43.820 に答える