2016-05-24 11 views
0

私は連絡先テーブルを持つユーザーを作成しようとしています。私は宣言していない列が追加されているので、正しい方法でそれをやっているかどうかはわかりません。ASP.NET Core EF多対多参照テーブル

エンティティ:

public class User 
{ 
    public int Id { get; set; } 
    public bool IsAvailable { get; set; } 
    public List<Contact> Contacts { get; set; } 
} 

public class Contact 
{ 
    public int UserId { get; set; } 
    public int ContactUserId { get; set; } 

    public User User { get; set; } 
    public User ContactUser { get; set; } 
} 

マッピング:

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Contact>() 
     .HasKey(x => new { x.UserId, x.ContactUserId }); 

    modelBuilder.Entity<Contact>() 
     .HasOne(x => x.User) 
     .WithMany(x => x.Contacts) 
     .HasForeignKey(x => x.UserId); 

    modelBuilder.Entity<Contact>() 
     .HasOne(x => x.ContactUser) 
     .WithMany(x => x.Contacts) 
     .HasForeignKey(x => x.ContactUserId); 
} 

結果:

migrationBuilder.CreateTable(
    name: "Contact", 
    columns: table => new 
    { 
     UserId = table.Column<int>(nullable: false), 
     ContactUserId = table.Column<int>(nullable: false), 
     UserId1 = table.Column<int>(nullable: true) 
    }, 
    constraints: table => 
    { 
     table.PrimaryKey("PK_Contact", x => new { x.UserId, x.ContactUserId }); 
     table.ForeignKey(
      name: "FK_Contact_User_ContactUserId", 
      column: x => x.ContactUserId, 
      principalTable: "User", 
      principalColumn: "Id", 
      onDelete: ReferentialAction.Cascade); 
     table.ForeignKey(
      name: "FK_Contact_User_UserId1", 
      column: x => x.UserId1, 
      principalTable: "User", 
      principalColumn: "Id", 
      onDelete: ReferentialAction.Restrict); 
    }); 

本当の問題:

が接触した列USERID1が来ていますから?私の定義に何か問題はありますか?ありがとう!あなたは間違っているユーザーオブジェクトのコンタクトする連絡先オブジェクトの両方ユーザーContactUser団体の反対側を指定しているので、あなたが接触テーブルに追加USERID1になってしまった理由がある

+0

をあなたがしようとしています何をするか:nか1:nの関係?あなたのモデルから、1人のユーザーがn人の連絡先を持っているように見えますが、これは誤解されていなければ、多くはないが1対多です。 – HotTowelie

+0

@HotTowelie 1:nを 'Contact'に、次にn:1を' Contact.ContactUser'にします。したがって、 'User'はn:*自身*です。 –

+0

関連性があるかもしれません: 'User.Contacts'は、' User'から 'ContactUser's *へのナビゲーションプロパティと、同時にナビゲーションプロパティが逆になることはできません。それらのうちの1つは 'User'エンティティから構成され、もう1つはパラメータなし' HasMany() 'で構成されます。 –

答えて

2

。結果として、EFはそれを無視し、ユーザオブジェクト上の連絡先のためのさらに別の関連付けを作成し、連絡先表のUserId1列にマッピングします。この問題を解決するために

一つの方法は、ユーザオブジェクト上の連絡先の別のリストを作成し、それに応じてマップすることです:

public class User 
{ 
    public int Id { get; set; } 
    public bool IsAvailable { get; set; } 

    public List<Contact> Contacts { get; set; } 
    public List<Contact> ContactUsers { get; set; } 
} 

modelBuilder.Entity<Contact>() 
    .HasOne(x => x.User) 
    .WithMany(x => x.Contacts) 
    .HasForeignKey(x => x.UserId); 

modelBuilder.Entity<Contact>() 
    .HasOne(x => x.ContactUser) 
    .WithMany(x => x.ContactUsers) 
    .HasForeignKey(x => x.ContactUserId) 
    .OnDelete(DeleteBehavior.Restrict); 


希望スキーマ生成どちら:

enter image description here

+0

大変ありがとうございました。また、OnDeleteを追加してくれてありがとうございます! – Megamind

+0

あなたは大歓迎です:) –

関連する問題