Dbset.Add(entity) assigns an ID and this results in a exception

c# dbset entity-framework entity-framework-6

Question

for the subsequent courses:

public Car 
{
    public int ID { get; set; }
    public string Brand {get; set; }
}

Typically, when we:

Car c = new Car { Brand = "Jaguar" } ; // Point A
context.Cars.Add(c); // Point B
context.SaveChanges() // Point C

The ID should stay 0 at point B, and only at point C should an ID be assigned. However, I have discovered that point B assigns an ID to one of my classes, which causes this exception to be thrown:

Cannot insert explicit value for identity column in table 'Cars' when IDENTITY_INSERT is set to OFF.

I've experimented with Fluent API, and I'm almost certain that I've specified my connections correctly. Why this DbSet attempts to assign an ID for this entity is beyond my comprehension.

Update

I appreciate your assistance, so here is a more thorough explanation of my predicament:

public Car 
{
    public int ID { get; set; }
    public string Brand {get; set; }
    public int Driver1ID {get; set;}
    public Person Driver1 {get; set;}
    public int Driver2ID {get; set;}
    public Person Driver2 {get; set;}
}

public Person
{
   public int ID { get; set; }
   public string Name { get; set; }
}

And this is how I speak fluently:

modelBuilder.Entity<Car>().HasKey(x => x.ID);
            modelBuilder.Entity<Car>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); // Added following Igor's suggestion
            modelBuilder.Entity<Car>().HasRequired(x => x.Driver1).WithOptional().WillCascadeOnDelete(false);
            modelBuilder.Entity<Car>().HasRequired(x => x.Driver2).WithOptional().WillCascadeOnDelete(false);

Edit 2

Well, I discovered that Migrations had indeed gone awry. Driver2, the second foreign key, was placed on the primary key column for some reason by EF. DbSet, this is why. The value that Add() was adding to the ID column was really the Driver 2 ID.

I have no idea how EF became so perplexed. The strange thing is that when I looked in SQL Management Studio, I didn't see this FK. It appears that EF entered certain relationships that weren't actually present in the database.

I deleted the _migrationhistory table and the migration folder, then used PowerShell to run Enable-Migrations and Add-Migration Init. This allowed me to view the problematic lines in the first migration file.

Of course, I changed them, and it appears to have fixed the issue.

1
0
2/20/2015 3:15:06 PM

Popular Answer

You can indicate whether or not the ID is assigned by the database using Identity in your fluent (and also declaratively) mapping. Your code shouldn't assign it if you state that it IS allocated since else you will get an exception. You may say it like this in fluent:

public Car 
{
    public int ID { get; set; }
    public string Brand {get; set; }
    public int Driver1ID {get; set;}
    public Person Driver1 {get; set;}
    public int Driver2ID {get; set;}
    public Person Driver2 {get; set;}
}

public Person
{
   public int ID { get; set; }
   public string Name { get; set; }
}


protected override void OnModelCreating(DbModelBuilder modelBuilder) { 
// ....
modelBuilder.Entity<Car>().HasKey(x => x.ID);
modelBuilder.Entity<Car>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); // set it on
modelBuilder.Entity<Car>().HasRequired(x => x.Driver1).WithMany().HasForeignKey(x => x.Driver1ID).WillCascadeOnDelete(false);
modelBuilder.Entity<Car>().HasRequired(x => x.Driver2).WithMany().HasForeignKey(x => x.Driver2ID).WillCascadeOnDelete(false);
// ....
}
0
2/20/2015 11:57:05 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow