【问题标题】:Entity Framework 6 inverting column order for composite foreign keys实体框架 6 反转复合外键的列顺序
【发布时间】:2015-07-23 17:30:47
【问题描述】:

我收到此错误:

(15,10):错误 3015:从第 6、15 行开始映射片段时出现问题:从表 Beta(Alpha_2,Alpha_1)到表 Alpha(Alpha_1,Alpha_2)的外键约束“Beta_Alpha”::映射不足:外键必须映射到一些在概念方面参与外键关联的 AssociationSet 或 EntitySet。

关键信息是Beta中的复合外键(Alpha_2,Alpha_1)的列颠倒了,Alpha_2在前,Alpha_1在后。这似乎是实体框架中的一个错误。

以下是实体:

public class Alpha
{
    [Key, Column(Order = 1)]
    public string Alpha_1 { get; set; }
    [Key, Column(Order = 2)]
    public string Alpha_2 { get; set; }
}

public class Beta
{
    [Key, Column(Order = 1)]
    public string Beta_1 { get; set; }
    [Key, Column(Order = 2)]
    public string Alpha_2 { get; set; }
    [Required]
    public string Alpha_1 { get; set; }

    [ForeignKey("Alpha_1, Alpha_2")]
    public virtual Alpha Alpha { get; set; }
}

请注意,Beta 中的 ForeignKey 属性正确地首先列出了 Alpha_1。

这是生成的迁移:

public partial class Test : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Betas",
            c => new
                {
                    Beta_1 = c.String(nullable: false, maxLength: 128),
                    Alpha_2 = c.String(nullable: false, maxLength: 128),
                    Alpha_1 = c.String(nullable: false, maxLength: 128),
                })
            .PrimaryKey(t => new { t.Beta_1, t.Alpha_2 })
            .ForeignKey("dbo.Alphas", t => new { t.Alpha_2, t.Alpha_1 }, cascadeDelete: true)
            .Index(t => new { t.Alpha_2, t.Alpha_1 });

        CreateTable(
            "dbo.Alphas",
            c => new
                {
                    Alpha_1 = c.String(nullable: false, maxLength: 128),
                    Alpha_2 = c.String(nullable: false, maxLength: 128),
                })
            .PrimaryKey(t => new { t.Alpha_1, t.Alpha_2 });

    }

    public override void Down()
    {
        DropForeignKey("dbo.Betas", new[] { "Alpha_2", "Alpha_1" }, "dbo.Alphas");
        DropIndex("dbo.Betas", new[] { "Alpha_2", "Alpha_1" });
        DropTable("dbo.Alphas");
        DropTable("dbo.Betas");
    }
}

即使我手动更新迁移,我仍然会收到错误,因为内部模型本身正在反转列。

该错误似乎与 Alpha_2 也是 Beta 复合主键的一部分这一事实有关。

更新研究了下面 Andrey Molotkov 提供的链接,我从 Beta 的 Alpha 属性中删除了 ForeignKey("Alpha_1, Alpha_2") 属性,并尝试在 Fluent API 中显式映射它,但它仍然有完全相同的问题。

该链接的下一个建议是使用 Column 属性显式设置顺序。我认为这正是问题的关键所在。我有一个属性 Alpha_2,它参与了一个复合主键和一个复合外键,所以我必须能够独立指定每个键的顺序。

public class Beta
{
    [Key, Column(Order = 1)]
    public string Beta_1 { get; set; }
    [Key, Column(Order = 2), ForeignKey("Alpha"), Column(Order = 1)]
    public string Alpha_2 { get; set; }
    [Required, ForeignKey("Alpha"), Column(Order = 2)]
    public string Alpha_1 { get; set; }

    public virtual Alpha Alpha { get; set; }
}

但这会导致错误,因为 Column 属性只能出现一次。

我认为 EF 需要一种方法来直接在 Key 或 ForeignKey 属性中指定 Order。

【问题讨论】:

  • 查看类似问答:stackoverflow.com/a/14062545/5114198
  • 感谢您的链接,安德烈。它可以帮助我更清楚地理解问题,但我仍然没有找到解决办法。

标签: entity-framework-6 composite-key


【解决方案1】:

你需要改变Beta外键列的顺序:

public class Beta
{
    [Key, Column(Order = 1)]
    public string Beta_1 { get; set; }
    [Key, Column(Order = 3), ForeignKey("Alpha")]
    public string Alpha_2 { get; set; }
    [Required, ForeignKey("Alpha"), Column(Order = 2)]
    public string Alpha_1 { get; set; }

    public virtual Alpha Alpha { get; set; }
}

【讨论】:

  • 再次感谢安德烈。我刚刚验证了这个工作。我还认为我用于数据完整性的一些外键在实体模型中并不是真正必要的,所以我可能不会遇到无法以正确的顺序排列列以支持所有外键。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 2016-05-06
  • 1970-01-01
相关资源
最近更新 更多