【问题标题】:Entity Framework can't create foreign key constraint, "There are no primary or candidate keys in the referenced table..."实体框架无法创建外键约束,“引用的表中没有主键或候选键...”
【发布时间】:2017-01-09 18:45:16
【问题描述】:

我正在尝试在 File 和 AMCN 这两个模型之间创建一对多的关系。这是我的文件:

public class File
{
    public int Id { get; set; }

    public int AmcnId { get; set; }
    [StringLength(255)]
    public string FileName { get; set; }
    [StringLength(100)]
    public string ContentType { get; set; }
    public byte[] Content { get; set; }

    public virtual AMCN Amcn { get; set; }
}

这是我的 AMCN 的一部分:

public class AMCN
{
    public int Id { get; set; }
    public DateTime Created { get; set; }
    //lots of other data here...
    public virtual ICollection<File> Files { get; set; }
    public virtual ICollection<FoSL> FoSLs { get; set; }
}

所以我进入包管理器控制台,执行add-migration files,它工作并生成了这个:

public partial class files : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Files",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    AmcnId = c.Int(nullable: false),
                    FileName = c.String(maxLength: 255),
                    ContentType = c.String(maxLength: 100),
                    Content = c.Binary(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.AMCNs", t => t.AmcnId, cascadeDelete: true)
            .Index(t => t.AmcnId);

    }

    public override void Down()
    {
        DropForeignKey("dbo.Files", "AmcnId", "dbo.AMCNs");
        DropIndex("dbo.Files", new[] { "AmcnId" });
        DropTable("dbo.Files");
    }
}

但是当我做update-database 时,我得到一个错误:

Error Number:1776,State:0,Class:16
There are no primary or candidate keys in the referenced table 'dbo.AMCNs' that match the referencing column list in the foreign key 'FK_dbo.Files_dbo.AMCNs_AmcnId'.
Could not create constraint. See previous errors.

据我所知,实体框架应该能够弄清楚我希望 AmcnId 是指向 AMCN.Id 的外键。事实上,当我制作另一个课程 FoSL 时,它运行良好。

public class FoSL
{
    public int Id { get; set; }
    public int AmcnId { get; set; }
    public string FO { get; set; }
    public DateTime StartDate { get; set; }
    public string Location { get; set; }
}

生成的迁移类

public partial class FoSL : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.FoSLs",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    AmcnId = c.Int(nullable: false),
                    FO = c.String(),
                    StartDate = c.DateTime(nullable: false),
                    Location = c.String(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.AMCNs", t => t.AmcnId, cascadeDelete: true)
            .Index(t => t.AmcnId);

    }

    public override void Down()
    {
        DropForeignKey("dbo.FoSLs", "AmcnId", "dbo.AMCNs");
        DropIndex("dbo.FoSLs", new[] { "AmcnId" });
        DropTable("dbo.FoSLs");
    }
}

我看不出FoSLFile 类或它们的迁移之间有任何真正的区别。据我所知,这应该有效。那我做错了什么?

我尝试在AMCN.Id 上添加[Key],但没有帮助。

编辑:最初的 dbmigration 包括 Up()

        CreateTable(
            "dbo.AMCNs",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Created = c.DateTime(nullable: false),
                    //lots of other data here....
                })
            .PrimaryKey(t => t.Id);

【问题讨论】:

  • 如果你不使用Fluent API配置,你必须正确使用ef code first conventions name。
  • 导致错误的“AMCN”迁移类在哪里,您的错误说 AMCN 表没有主键..首先检查该代码
  • @KarthikGanesan 编辑了包含它的问题,M.Wiśnicki 不是吗?
  • 迁移将在上下文创建之前更新数据库,懒惰的引导与该问题无关
  • 在文件类 [ForeignKey("AMCN") 的 AmcnId 上键入此 arrtibute

标签: entity-framework sql-server-2008


【解决方案1】:

原来罪魁祸首是我忘记的;我制作了一个备份数据库进行测试,因此在我准备好之前不会影响生产数据库。但是,当我将数据备份到新数据库时,它并没有带上密钥。所以我在数据库中的 AMCN 表没有实体框架所期望的主键。我添加了一个主键,再次在包管理器控制台中运行update-database,它运行良好。

(不确定我是否应该删除这个问题...)

【讨论】:

  • 好吧,如果您认为它对任何人都没有帮助,因为问题只是您犯了一个错误,也许您应该这样做,但我不是版主
【解决方案2】:

发生在亚当身上的完全相同的事情也发生在我身上。我创建了一个备份数据库来测试更改而不影响我的生产环境。使用 MS SSMS 上的 SQL 导入/导出向导进行复制,这意味着表的 PK 信息没有被复制,因此迁移工作,但尝试更新数据库没有,因为没有明确的 PK 可能是找到了。

在 SSMS 表设计器中手动配置 PK,迁移工作完美。发帖以防有人犯了和我一样的错误!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-06
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2012-08-26
    相关资源
    最近更新 更多