C# - 外键引用无效 table - 基本迁移不工作

C# - Foreign key references invalid table - Basic Migration not working

尝试用 C# 制作我的第一个项目。但是,到目前为止,这非常令人沮丧。这是我无法解决的问题,因为我没有发现任何错误。

我正在尝试在我的数据库中进行简单的迁移。

用户迁移文件

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

每次我想迁移时都会遇到同样的错误

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 名称 属性 名称:

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

这样您就可以告诉它哪个导航 属性 代表它作为外键的关系。

之后,删除旧迁移并再次尝试 运行 Add-Migration 命令重新生成它。您现在应该看到 CreateIndex 方法在 AddForeignKey 方法之前被调用:

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

我最近遇到了这个问题。

我删除了所有迁移,运行 删除迁移几次以清理一些默认的 ASP.Net 身份迁移,然后从头开始重新创建迁移。

Entity Framework Core 混淆了 IdentityUser class 的默认脚手架和我的自定义实现,因此当我去创建 [=23] 时找不到 table ASPNetUsers =] 与之相关,在本例中为 Referrals.

在使用数据库内置身份的 EF Core 时,请务必清除所有默认迁移和 运行 删除迁移。

希望这对困惑的人有所帮助。