实体框架6反转列复合外键列 [英] Entity Framework 6 inverting column order for composite foreign keys

查看:331
本文介绍了实体框架6反转列复合外键列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到这个错误:



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



关键信息是Beta(Alpha_2,Alpha_1)中的复合外键具有反转的列,Alpha_2第一和Alpha_1秒。这似乎是实体框架中的一个错误。



以下是实体:

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

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

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

请注意,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: maxLength:128),
})
.PrimaryKey(t => new {t.Beta_1,t.Alpha_2})
.ForeignKey(dbo.Alphas,t =>新的{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;组;
[Key,Column(Order = 2),ForeignKey(Alpha),Column(Order = 1)]
public string Alpha_2 {get;组; }
[必需,ForeignKey(Alpha),列(Order = 2)]
public string Alpha_1 {get;组; }

public virtual Alpha Alpha {get;组; }
}

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

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

解决方案

您需要更改 Beta 外键列的顺序:

  public class Beta 
{
[Key,Column(Order = 1)]
public string Beta_1 {get;组; }
[Key,Column(Order = 3),ForeignKey(Alpha)]
public string Alpha_2 {get;组; }
[必需,ForeignKey(Alpha),列(Order = 2)]
public string Alpha_1 {get;组; }

public virtual Alpha Alpha {get;组; }
}


I'm getting this error:

(15,10) : error 3015: Problem in mapping fragments starting at lines 6, 15: Foreign key constraint 'Beta_Alpha' from table Beta (Alpha_2, Alpha_1) to table Alpha (Alpha_1, Alpha_2):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.

The key information is that the composite foreign key in Beta (Alpha_2, Alpha_1) has the columns inverted, with Alpha_2 first and Alpha_1 second. This appears to be a bug in Entity Framework.

Here are the entities:

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

Note that the ForeignKey attribute in Beta correctly lists Alpha_1 first.

Here is the Migration that's generated:

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

Even if I manually update the Migration, I still get the error because the internal model itself is inverting the columns.

The bug appears to be related to the fact that Alpha_2 is also part of Beta's composite primary key.

Update Studying the link provided by Andrey Molotkov below, I removed the ForeignKey("Alpha_1, Alpha_2") attribute from Beta's Alpha property and tried to explicitly map it in the Fluent API, but it still had the exact same problem.

The next suggestion at that link was to explicitly set the order using the Column attribute. I think this gets to the crux of the problem. I have a property, Alpha_2, that is participating in a composite primary key, and a composite foreign key, so I would have to be able to specify the order for each key independently.

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

But this causes an error because the Column attribute can only appear once.

I think EF needs a way to specify the Order directly in the Key or ForeignKey attribute.

解决方案

You need to change the order of Beta foreign key columns:

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

这篇关于实体框架6反转列复合外键列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆