Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF代码首先与ASP.NET标识表建立多对多关系_C#_Asp.net_Asp.net Mvc_Entity Framework_Ef Code First - Fatal编程技术网

C# EF代码首先与ASP.NET标识表建立多对多关系

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;

我正在尝试使用代码优先EF和ASP.NET标识表实现多对多关系。不过,联接表不是在数据库中生成的。我错过了什么?以下是我的模型课程:

AppUser.cs:

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的情况下首先创建联接表。