2017-04-09 29 views
0

ASP.NET Identityテーブルを持つコードファーストEFを使用して多対多の関係を達成しようとしています。ただし、DB内では結合テーブルは生成されません。私は何が欠けていますか?EFコード最初の多対多のASP.NET IDテーブルとの関係

AppUser.cs:

public class AppUser : IdentityUser 
{ 
    public AppUser() 
    { 
     Notes = new HashSet<Note>(); 
    } 

    public DateTime? LastSuccessfulLoginDate { get; set; } 

    public virtual ICollection<Note> Notes { get; set; } 
} 

Note.cs:あなたのdbcontextで

public class Note 
{ 
    public Note() { 

     NoteAssignedToUsers = new HashSet<AppUser>(); 
    } 

    [Key] 
    public int NoteID { get; set; } 

    [Required] 
    public int FileID { get; set; } 

    [Required] 
    public string Content { get; set; } 

    [Required] 
    public Importance? Importance { get; set; } 

    [Required] 
    [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy HH.mm}")] 
    public DateTime AddedOn { get; set; } 

    [Required] 
    public string CreatorID { get; set; } 

    [ForeignKey("FileID")] 
    public virtual OAFile OAFile { get; set; } 

    [ForeignKey("CreatorID")] 
    public virtual AppUser CreatedBy { get; set; } 

    public virtual ICollection<AppUser> NoteAssignedToUsers { get; set; } 
} 

答えて

1

あなたが設定できます。ここに私のモデルクラスは

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 

    modelBuilder.Entity<Note>() 
       .HasMany<AppUser>(s => s.NoteAssignedToUsers) 
       .WithMany(c => c.Notes) 
       .Map(cs => 
         { 
          cs.MapLeftKey("AppUserId"); 
          cs.MapRightKey("NoteId"); 
          cs.ToTable("AppUsersNotes"); 
         }); 

} 
+0

ありがとう、デイ、これはFluent APIです。どのようにコードファーストでそれを設定できますか? – burakk

+0

どちらもコードは最初に流暢なAPIまたはデータ注釈でコードを使用することができます –

+0

はい、私はデータアノテーションを意味しました。私は、EFがFluent APIを必要とせずに最初に結合テーブルを作成しなかった理由を知りたいと思います。 – burakk

関連する問題