C# - 外键引用无效的表 - 基本迁移不工作 [英] C# - Foreign key references invalid table - Basic Migration not working
问题描述
试图让我在C#中的第一个项目。然而,这是非常令人沮丧为止。这是我解决不了,因为我看不出有什么不对的问题。
Trying to make my first project in C#. However, it's very frustrating so far. This is a problem I can't solve, since I don't see anything wrong.
我想在我的DB只是做一个简单的迁移。
I'm trying to just do a simple migrate in my DB.
用户迁移文件
public override void Up()
{
CreateTable(
"dbo.Users",
c => new
{
user_id = c.Int(nullable: false, identity: true),
first_name = c.String(),
last_name = c.String(),
email = c.String(),
role = c.String(),
})
.PrimaryKey(t => t.user_id);
}
位置迁移文件
public override void Up()
{
CreateTable(
"dbo.Locations",
c => new
{
loc_id = c.Int(nullable: false, identity: true),
loc_name = c.String(),
})
.PrimaryKey(t => t.loc_id);
CreateTable(
"dbo.UserLoc",
c => new
{
ul_id = c.Int(nullable: false, identity: true),
fk_user_id = c.Int(nullable: false),
fk_loc_id = c.Int(nullable: false),
})
.PrimaryKey(t => t.ul_id);
AddForeignKey("dbo.UserLoc", "fk_user_id", "dbo.Users", "user_id");
AddForeignKey("dbo.UserLoc", "fk_loc_id", "dbo.Locations", "loc_id");
}
的模型:的
User.cs
public class User
{
[Key]
public int user_id { get; set; }
public string firs_tname { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string role { get; set; }
}
Location.cs
public class Locations
{
[Key]
public int loc_id { get; set; }
public int loc_name { get; set; }
}
UserLoc.cs
public class UserLoc
{
[Key]
public int ul_id { get; set; }
[ForeignKey("Users")]
public int fk_user_id { get; set; }
public virtual User user { get; set; }
[ForeignKey("Locations")]
public int fk_location_id { get; set; }
public virtual Locations location { get; set; }
}
我每次要迁移,我得到了同样的错误。
Every time I want to migrate I get the same error
外键'FK_dbo.UserLoc_dbo.Users_fk_user_id引用无效
表'dbo.Users。无法创建约束或索引。请参阅前面的
错误。
Foreign key 'FK_dbo.UserLoc_dbo.Users_fk_user_id' references invalid table 'dbo.Users'. Could not create constraint or index. See previous errors.
我在做什么错了?
推荐答案
首先,我建议你改变你使用了 ForeignKey的
属性导航属性名称FK名称:
First I recommend you to change the FK name that you are using in the ForeignKey
attributes for the navigation property names:
public class UserLoc
{
[Key]
public int ul_id { get; set; }
[ForeignKey("user")]
public int fk_user_id { get; set; }
public virtual User user { get; set; }
[ForeignKey("location")]
public int fk_location_id { get; set; }
public virtual Locations location { get; set; }
}
这样,你告诉它该导航属性代表的关系是一个外键的。
This way you are telling it which navigation property represents the relationship it is a foreign key for.
在此之后,删除旧的迁移,并尝试再次运行添加迁移
命令来重新生成它。您现在应该看到的CreateIndex
方法之前, AddForeignKey
调用的方法:
After that, remove the old migration and try to run again Add-Migration
command to regenerate it. You should see now that CreateIndex
method is called before AddForeignKey
method:
CreateIndex("dbo.UserLoc", "fk_user_id");
AddForeignKey("dbo.UserLoc", "fk_user_id", "dbo.Users", "user_id");
CreateIndex("dbo.UserLoc", "fk_loc_id");
AddForeignKey("dbo.UserLoc", "fk_loc_id", "dbo.Locations", "loc_id");
这篇关于C# - 外键引用无效的表 - 基本迁移不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!