C# EF代码首先与ASP.NET标识表建立多对多关系
我正在尝试使用代码优先EF和ASP.NET标识表实现多对多关系。不过,联接表不是在数据库中生成的。我错过了什么?以下是我的模型课程: AppUser.cs:C# EF代码首先与ASP.NET标识表建立多对多关系,c#,asp.net,asp.net-mvc,entity-framework,ef-code-first,C#,Asp.net,Asp.net Mvc,Entity Framework,Ef Code First,我正在尝试使用代码优先EF和ASP.NET标识表实现多对多关系。不过,联接表不是在数据库中生成的。我错过了什么?以下是我的模型课程: AppUser.cs: public class AppUser : IdentityUser { public AppUser() { Notes = new HashSet<Note>(); } public DateTime? LastSuccessfulLoginDate { get; set;
public class AppUser : IdentityUser
{
public AppUser()
{
Notes = new HashSet<Note>();
}
public DateTime? LastSuccessfulLoginDate { get; set; }
public virtual ICollection<Note> Notes { get; set; }
}
及
注2.cs:
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; }
}
在dbcontext中,您可以配置:
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");
});
}
谢谢Dei,但这是Fluent API。如何在“代码优先”中配置它?两者都是“代码优先”。您可以将“代码优先”与fluent api或数据注释一起使用。我知道,我指的是数据注释。我想知道为什么EF没有在没有Fluent API的情况下首先创建联接表。