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])
c# entity-framework ef-code-first
add a comment |
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])
c# entity-framework ef-code-first
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
add a comment |
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])
c# entity-framework ef-code-first
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
c# entity-framework ef-code-first
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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