Entity Framework error: one or more validation errors were detected during model generation with composite...











up vote
0
down vote

favorite












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])









share|improve this question
























  • Check out this link and see if it helps
    – ruffone
    Nov 9 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 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 at 21:42















up vote
0
down vote

favorite












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])









share|improve this question
























  • Check out this link and see if it helps
    – ruffone
    Nov 9 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 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 at 21:42













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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])









share|improve this question















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])






c# entity-framework ef-code-first






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 22:18

























asked Nov 7 at 22:46









Jefff

13




13












  • Check out this link and see if it helps
    – ruffone
    Nov 9 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 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 at 21:42


















  • Check out this link and see if it helps
    – ruffone
    Nov 9 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 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 at 21:42
















Check out this link and see if it helps
– ruffone
Nov 9 at 23:30




Check out this link and see if it helps
– ruffone
Nov 9 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 at 14:16




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 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 at 21:42




So it looks like the answer is you cannot have overlapping keys. I'll have to change the DB.
– Jefff
Nov 12 at 21:42

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199066%2fentity-framework-error-one-or-more-validation-errors-were-detected-during-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199066%2fentity-framework-error-one-or-more-validation-errors-were-detected-during-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini