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
LayoutModelwith the sameIdvar 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