问题描述
在使用Entity Framework Core 3.1.7为MysqL创建了这样的模型之后,我收到一个尝试运行迁移的错误; dotnet ef数据库更新。原因是所有索引都在迁移结束时添加。我是否应该经历一下并在CREATE TABLE之后手动放置索引,还是一个bug?试图整合我所有的迁移。
创建表Children
(
CommonId
int NOT NULL,
ParentId
int NOT NULL,
ChildId
int NOT NULL,
Group_Roles
int NULL,
约束PK_Children
主键(CommonId
,ParentId
,ChildId
),
删除级联时约束FK_Children_Parents_CommonId_ParentId
外键(CommonId
,ParentId
)参考Parents
(CommonId
,ParentId
)
);
MysqL.Data.MysqLClient.MysqLException(0x80004005):无法添加外键约束。参照表“父母”中缺少约束的索引“ FK_Children_Parents_CommonId_ParentId”
---> MysqL.Data.MysqLClient.MysqLException(0x80004005):添加外键约束失败。引用表“父母”中约束“ FK_Children_Parents_CommonId_ParentId”的缺少索引
public class Parent
{
public int CommonId { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public virtual ICollection<Child> Children { get; } = new HashSet<Child>();
}
public class Child
{
public int CommonId { get; set; }
public int ParentId { get; set; }
public int ChildId { get; set; }
public virtual Parent Parent { get; set; }
}
public class TestContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMysqL("");
}
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Parent>(entity =>
{
entity.HasKey(e => new { e.CommonId,e.ParentId });
entity.Property(e => e.ParentId)
.ValueGeneratedOnAdd();
});
modelBuilder.Entity<Parent>().HasIndex(e => new { e.CommonId,e.ParentId }).IsUnique();
modelBuilder.Entity<Child>(entity =>
{
entity.HasKey(e => new { e.CommonId,e.ParentId,e.ChildId });
entity.HasOne(e => e.Parent)
.WithMany(e => e.Children)
.HasForeignKey(p => new { p.CommonId,p.ParentId });
});
}
}
是要手动在两个表之间移动modelBuilder.CreateIndex吗?
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Parents",columns: table => new
{
CommonId = table.Column<int>(nullable: false),ParentId = table.Column<int>(nullable: false)
.Annotation("MysqL:ValueGenerationStrategy",MysqLValueGenerationStrategy.IdentityColumn),Name = table.Column<string>(nullable: true)
},constraints: table =>
{
table.PrimaryKey("PK_Parents",x => new { x.CommonId,x.ParentId });
});
migrationBuilder.CreateTable(
name: "Children",ParentId = table.Column<int>(nullable: false),ChildId = table.Column<int>(nullable: false),Group_Roles = table.Column<int>(nullable: true)
},constraints: table =>
{
table.PrimaryKey("PK_Children",x.ParentId,x.ChildId });
table.ForeignKey(
name: "FK_Children_Parents_CommonId_ParentId",columns: x => new { x.CommonId,x.ParentId },principalTable: "Parents",principalColumns: new[] { "CommonId","ParentId" },onDelete: referentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Parents_CommonId_ParentId",table: "Parents",columns: new[] { "CommonId",unique: true);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)