0

I am getting validation errors when using Entity Framework 6.2. The code was generated by using code-first from an existing database. The primary key and foreign key are both composite and use version. The code has been sanitized to just show the relevant parts. It looks like it is trying to swap the version and ID values. Any suggestions would be greatly appreciated.

public partial class MyParent
{
    public MyParent()
    {
        MyChild = new HashSet< MyChild >();
    }

    [Key]
    [Column(Order = 0)]
    [StringLength(50)]
    public string MyID { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int MyVersion { get; set; }
}

public partial class MyChild
{
    [Key]
    [Column(Order = 0)]
    [StringLength(50)]
    public string ChildID { get; set; }

    [Required]
    [StringLength(50)]
    [ForeignKey("MyParent) Column(Order = 1)]
    public string MyID { get; set; }

    public string NextChild { get; set;}

    [Key]
    [Column(Order = 2)]
    [ForeignKey("MyParent")]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int MyVersion { get; set; }

    public virtual MyParent MyParent { get; set; }
    public virtual ICollection<MyChild> MyChilds1 { get; set; }

    public virtual MyChild MyChild1 { get; set; }
}

public partial class MyContext : DbContext
{
    public MyContext()
        : base("name= MyContext ")
    {
    }

    public MyContext (string connectionString) : base(connectionString)
    {
    }

    public virtual DbSet<MyParent> MyParent { get; set; }
    public virtual DbSet<MyChild> myChild { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyParent>()
            .HasMany(e => e.MyChild)
            .WithRequired(e => e.MyParent)
            .HasForeignKey(e => new { e.MyID, e.MyVersion })
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<MyChild>()
            .HasMany(e => e.MyChilds1)
            .WithOptional(e => e.MyChild1)
            .HasForeignKey(e => new { e.ChildID, e.MyVersion });
    }
}

Error:

"One or more validation errors were detected during model generation: MyParent_MyChild: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property MyVersion on entity ‘MyChild’ does not match the type of property ‘MyID’ on entity ‘MyParent’ in the referential constraint ‘MyParent_MyChild'.

MyParent_MyChild: The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property ‘ChildID’ on entity ‘MyChild’ does not match the type of property ‘MyVersion’ on entity ‘MyParent’ in the referential constraint ‘MyParent_MyChild’.

I have added a fix for the simple case. It was to add ForeignKey tags and adjust the column sequence. This does not work on more complex models i.e. self referencing tables (NextChild).

New Error:

$exception{"The navigation property 'MyChild' declared on type 'MyChild.MyChilds1 has been configured with conflicting foreign keys."} System.InvalidOperationException

The db code for the foreign keys is:

    [PK_MyChild] PRIMARY KEY CLUSTERED ([ChildID] ASC, [MyVersion] ASC),
    [FK_MyChild_MyParent] FOREIGN KEY ([MyID], [MyVersion]) REFERENCES [dbo].[MyParent]([MyID], [MyVersion]),
    [FK_MyChild_MyChild] FOREIGN KEY ([NextChild], [MyVersion]) REFERENCES [dbo].[MyChild] ([ChildID], [MyVersion])
Jefff
  • 1
  • 3
  • Check out this [link](https://stackoverflow.com/questions/7050404/create-code-first-many-to-many-with-additional-fields-in-association-table?rq=1) and see if it helps – ruffone Nov 09 '18 at 23:30
  • Thank you ruffone. It doesn't quite cover the situation I'm in where multiple composite FK in a single table use the same column as a part of their definition. – Jefff Nov 12 '18 at 14:16
  • So it looks like the answer is you cannot have overlapping keys. I'll have to change the DB. – Jefff Nov 12 '18 at 21:42

0 Answers0