gpt4 book ai didi

mysql - EF5 :could not be created because the principal key columns could not be determined. 使用 AddForeignKey 流畅 API 完全指定外键

转载 作者:行者123 更新时间:2023-12-02 01:05:04 28 4
gpt4 key购买 nike

我们正在使用Entity Framework 5.0。和数据库MySQL。当我们尝试迁移时间时出现异常。

could not be created because the principal key columns could not be determined. Use the AddForeignKey fluent API to fully specify the Foreign Key.

最佳答案

如此处所述 http://www.ozkary.com/2015/02/the-foreign-key-on-table-with-columns.html ,将 FK 放入不属于您的数据库上下文的表时可能会遇到问题

所以而不是这样做

CreateTable(
"dbo.App",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 150),
RoleId = c.String()
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.MyRoles", t => t.RoleId)
.Index(t => t.RoleId);

你应该这样做:

CreateTable(
"dbo.App",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 150),
RoleId = c.String()
})
.PrimaryKey(t => t.Id)
.Index(t => t.RoleId);

AddForeignKey("dbo.App", "RoleId", "dbo.Roles","Id");

关于mysql - EF5 :could not be created because the principal key columns could not be determined. 使用 AddForeignKey 流畅 API 完全指定外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15430981/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com