Entity framework MVC5代码首先将用户添加到多个关系中

Entity framework MVC5代码首先将用户添加到多个关系中,entity-framework,ef-code-first,asp.net-mvc-5,asp.net-identity,Entity Framework,Ef Code First,Asp.net Mvc 5,Asp.net Identity,我使用mvc5与codefirst和用户帐户的东西 我有一个简单的要求,一个用户可以属于多个商店,一个企业可以有多个商店 到目前为止,我已经有了这项业务——许多商店都在运作,但我似乎不知道如何设置用户 我的店面模型 public virtual Business Business { get; set; } public virtual ICollection<ApplicationUser> Employees { get; set; } 有人能帮我做些什么来

我使用mvc5与codefirst和用户帐户的东西

我有一个简单的要求,一个用户可以属于多个商店,一个企业可以有多个商店

到目前为止,我已经有了这项业务——许多商店都在运作,但我似乎不知道如何设置用户

我的店面模型

    public virtual Business Business { get; set; }

    public virtual ICollection<ApplicationUser> Employees { get; set; } 

有人能帮我做些什么来让这项工作正常进行吗?

你可以不用一个存储表,在每个appuser和business中列出一个列表,EF将建立你的多对多关系,或者你可以使用fluent api映射一切。应该是如下所示

 public class Business
{
    public virtual ICollection<StoreModel> Stores { get; set; }
}
public class ApplicationUser : IdentityUser
{
    public virtual ICollection<StoreModel> Stores { get; set; }
}




public class StoreModel {
public string ApplicationUserId { get; set; }
public int BusinessId { get; set; }

public virtual Business Businesss { get; set; }
public virtual ApplicationUser ApplicationUsers { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{   base.OnModelCreating(modelBuilder);
modelBuilder.Entity<StoreModel>().HasKey(e => new { e.ApplicationUserId, e.BusinessId});
}
公共类业务
{
公共虚拟ICollection存储{get;set;}
}
公共类应用程序用户:IdentityUser
{
公共虚拟ICollection存储{get;set;}
}
公共类存储模型{
公共字符串applicationSerID{get;set;}
public int BusinessId{get;set;}
公共虚拟业务业务{get;set;}
公共虚拟应用程序用户应用程序用户{get;set;}
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{base.OnModelCreating(modelBuilder);
modelBuilder.Entity().HasKey(e=>new{e.applicationserid,e.BusinessId});
}

您可以不使用存储表,在每个appuser和business中列出一个列表,EF将建立多对多关系,或者您可以使用fluent api映射所有内容。应该是如下所示

 public class Business
{
    public virtual ICollection<StoreModel> Stores { get; set; }
}
public class ApplicationUser : IdentityUser
{
    public virtual ICollection<StoreModel> Stores { get; set; }
}




public class StoreModel {
public string ApplicationUserId { get; set; }
public int BusinessId { get; set; }

public virtual Business Businesss { get; set; }
public virtual ApplicationUser ApplicationUsers { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{   base.OnModelCreating(modelBuilder);
modelBuilder.Entity<StoreModel>().HasKey(e => new { e.ApplicationUserId, e.BusinessId});
}
公共类业务
{
公共虚拟ICollection存储{get;set;}
}
公共类应用程序用户:IdentityUser
{
公共虚拟ICollection存储{get;set;}
}
公共类存储模型{
公共字符串applicationSerID{get;set;}
public int BusinessId{get;set;}
公共虚拟业务业务{get;set;}
公共虚拟应用程序用户应用程序用户{get;set;}
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{base.OnModelCreating(modelBuilder);
modelBuilder.Entity().HasKey(e=>new{e.applicationserid,e.BusinessId});
}

您可能需要编写一些
FluentAPI
,以获得您个人想要的东西,当我们使用fluent api时,如果不使用
FluentAPI
@SOfanatic,我永远无法让多对多按我所希望的方式工作。我们需要在模型中指定导航属性吗?您可能需要编写一些
FluentAPI
,以获得您个人想要的内容,如果不使用
FluentAPI
@SOfanatic,我永远无法让多对多的人按照我希望的方式工作。当我们使用fluent api时,我们需要在模型中指定导航属性吗?当我们使用fluent api时,我们需要在模型中指定导航属性吗?当我们使用fluent api时,我们需要指定导航属性吗模型中的属性?
  public override void Up()
    {
        RenameTable(name: "dbo.Stores", newName: "ApplicationUserStoreModels");
        DropForeignKey("dbo.Stores", "ApplicationUser_Id", "dbo.AspNetUsers");
        DropIndex("dbo.Stores", new[] { "ApplicationUser_Id" });
        CreateIndex("dbo.ApplicationUserStoreModels", "ApplicationUser_Id");
        CreateIndex("dbo.ApplicationUserStoreModels", "StoreModel_StoreId");
        AddForeignKey("dbo.ApplicationUserStoreModels", "ApplicationUser_Id", "dbo.AspNetUsers", "Id", cascadeDelete: true);
        AddForeignKey("dbo.ApplicationUserStoreModels", "StoreModel_StoreId", "dbo.Stores", "StoreId", cascadeDelete: true);
        DropColumn("dbo.Stores", "ApplicationUser_Id");
    }
 public class Business
{
    public virtual ICollection<StoreModel> Stores { get; set; }
}
public class ApplicationUser : IdentityUser
{
    public virtual ICollection<StoreModel> Stores { get; set; }
}




public class StoreModel {
public string ApplicationUserId { get; set; }
public int BusinessId { get; set; }

public virtual Business Businesss { get; set; }
public virtual ApplicationUser ApplicationUsers { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{   base.OnModelCreating(modelBuilder);
modelBuilder.Entity<StoreModel>().HasKey(e => new { e.ApplicationUserId, e.BusinessId});
}