我正在尝试在User和Request类之间创建多对多关系。迁移应该生成一个中间表,但它在两个表中生成一对多。
public class ApplicationUser : IdentityUser
{
...
public virtual ICollection<Request> Requests{ get; set; }
}
public partial class Request
{
...
public virtual ICollection<ApplicationUser> Users{ get; set; }
}
public partial class _12 : DbMigration
{
public override void Up()
{
AddColumn("dbo.AspNetUsers", "Request_Id", c => c.Int());
AddColumn("dbo.Requests", "ApplicationUser_Id", c => c.String(maxLength: 128));
CreateIndex("dbo.AspNetUsers", "Request_Id");
CreateIndex("dbo.Requests", "ApplicationUser_Id");
AddForeignKey("dbo.AspNetUsers", "Request_Id", "dbo.Requests", "Id");
AddForeignKey("dbo.Requests", "ApplicationUser_Id", "dbo.AspNetUsers", "Id");
}
...
}
答案 0 :(得分:0)
使用流畅的API工作。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>()
.HasMany(t => t.Requests)
.WithMany(t => t.Users);
}
给我这个
public partial class _12 : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.ApplicationUserRequests",
c => new
{
ApplicationUser_Id = c.String(nullable: false, maxLength: 128),
Request_Id = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.ApplicationUser_Id, t.Request_Id })
.ForeignKey("dbo.AspNetUsers", t => t.ApplicationUser_Id, cascadeDelete: true)
.ForeignKey("dbo.Requests", t => t.Request_Id, cascadeDelete: true)
.Index(t => t.ApplicationUser_Id)
.Index(t => t.Request_Id);
}
...
}
我不必使用流畅的api表格,我自己添加了,但用户表格不同。