1

コードファーストとユーザーアカウントのものでmvc5を使用しています。

ユーザーが複数の店舗に所属でき、ビジネスが複数の店舗を持つことができるという単純な要件があります。

これまでのところ、私はビジネスを行っています-多くの店舗が機能していますが、ユーザーをセットアップする方法がわかりません.

私のストアモデル

    public virtual Business Business { get; set; }

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

私のビジネスモデル

    public virtual ICollection<StoreModel> Stores { get; set; }

私の文脈の中で私は試しました

public class ApplicationUser : IdentityUser
{
    //a user can belong to multiple stores
    public virtual ICollection<StoreModel> Stores { get; set; }
}

ただし、add-migration を試行すると、生成されたコードによってテーブル名が変更され、Store と AspNetUser の間に結合テーブルが作成されます

私の移行は次のようになります

  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");
    }

これを機能させるために私がする必要があることを誰かが助けることができますか?

4

1 に答える 1

1

ストア テーブルを持たずに各 appuser とビジネスにリストを配置するだけで、EF が多対多の関係を構築するか、流暢な 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});
}
于 2013-10-22T20:03:02.083 に答える