0

Entity layout contains int value of venue (VenueId prop), its own id and other information.

CONSTRAINT [FK_Venue_Layout] FOREIGN KEY ([VenueId]) REFERENCES [dbo].[Venue] ([Id]) 

When I trying to add two layouts with the same VenueId, I'm getting this error

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Saving or accepting changes failed because more than one entity of type 'DataAccess.Models.LayoutModel' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration."

My entity code:

[Table("Layout")] public class LayoutModel { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key] public int Id { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.None)] public int VenueId { get; set; } public string Description { get; set; } } 

Insertion code:

var layouts = new List<LayoutModel> { new LayoutModel { VenueId = 1, Description = "First layout" }, new LayoutModel { VenueId = 1, Description = "Second layout" }, }; _context.Layouts.AddRange(layouts); _context.SaveChanges(); 

I'm not allowed to use navigation properties

5
  • You're probably trying to add 2 LayoutModel with the same Id Commented Jan 13, 2020 at 17:10
  • Can you try decorating the properties with [Column(Order = 0)] for Id and [Column(Order = 1)] for VenueId. Also, decorate VenueId using [ForeignKey("Venue")]. Also, could you paste INSERT values for Id, venueId combo? Commented Jan 13, 2020 at 17:13
  • @Magnetron they have differt id's Commented Jan 13, 2020 at 17:18
  • @sam var layouts = new List<LayoutModel> { new LayoutModel { Id = 1, VenueId = 1, Description = "First layout" }, new LayoutModel { Id = 2, VenueId = 1, Description = "Second layout" }, }; _context.Layouts.AddRange(layouts); _context.SaveChanges(); I have one Venue with Id one. I can't decorate decorate VenueId using [ForeignKey("Venue")] because i can't add navigation properies Commented Jan 13, 2020 at 17:18
  • @Nicefsf: Please update the post instead of comments. When Id is identity column, why are you explicitly setting Id to 1 and/or 2? Please remove that assignment statement. Commented Jan 13, 2020 at 17:24

1 Answer 1

1

Id column or property is marked as identity column in the definition of LayoutViewModel

[DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key] public int Id { get; set; } 

So, no need to assign it explicitly as it will be populated by Database automatically after the row is inserted into Layout table. Please update your layouts population as below to remove Id assignment:

var layouts = new List<LayoutModel> { new LayoutModel { /*Id = 1,*/ VenueId = 1, Description = "First layout" }, new LayoutModel { /*Id = 2, */ VenueId = 1, Description = "Second layout" } }; // code smell foreach(var layout in layouts) { context.Entry(layout).State = EntityState.Added; } _context.Layouts.AddRange(layouts); _context.SaveChanges(); 

Also, please update your LayoutModel as below:

public class LayoutModel { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key] [Column(Order = 0)] public int Id { get; set; } [Key] [Column(Order = 1)] //[ForeignKey("Venue")] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int VenueId { get; set; } //public virtual VenueModel Venue { get; set; } //Please correct Venue property type } 

Also, please verify whether Venue is loaded into _context.Layouts or not.

Sign up to request clarification or add additional context in comments.

9 Comments

I've added id's assignment just before pasting this code here
@Nicefsf, could you please update your post to depict the code that is actually throwing the exception. Thank you in advance.
@Nicefsf Could you please decorate VenueId with ForeignKey attribute?
To use [ForeignKey] i need to have Venue navigation property, but i'm not allowed to (task requirement) as i mentioned earlier
@Nicefsf Could you try manually updating EntityState as shown in my post.
|