Entity Framework 6 inverting column order for composite foreign keys

I am getting this error:

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

The key information is that the composite foreign key in beta (Alpha_2, Alpha_1) has reversed columns, Alpha_2 first and Alpha_1 second. This looks like 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 Beta ForeignKey attribute correctly maps Alpha_1 first.

Here is the generated migration:

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 inverts the columns.

The error stems from the fact that Alpha_2 is also part of the primary key of the beta compositions.

Refresh . After examining the link provided by Andrey Molotkov below, I removed the ForeignKey ("Alpha_1, Alpha_2") attribute from the Beta Alpha property and tried to explicitly map it to the Fluent API, but it still had the same problem.

The next suggestion at this link was to explicitly set the order using the Column attribute. I think this gets to the heart of the problem. I have a property Alpha_2 participating in a composite primary key and a composite foreign key, so I would have to define the order for each key myself.

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 raises 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.

+3


source to share


1 answer


You need to change the order of the Beta

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

      

+1


source







All Articles