The property 'BookId' on entity type 'Library' has a temporary value. Either set a permanent value explicitly or ensure that the database is

.net-core ef-migrations entity-framework entity-framework-core

Question

When I use Entity Framework Core and seed data with migration, I have this error:

The property 'BookId' on entity type 'Library' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.

1
2
2/18/2019 5:05:25 PM

Accepted Answer

The message means that you have given a value for your key that did not exist if you have dependencies with ForeignKey.

1
2/18/2019 5:05:25 PM

Popular Answer

I got the same exception when I had unique index, AddRange failed on unique index and then inside catch exception block was try to remove whole inserted collection. (Not my code, but I had to fix it :-) )

Code sample (simplified):

try {
    context.AddRange(users); // List<User>, has property List<Contact>
    context.SaveChanges(); // throws exception on unique index
} catch (Exception ex) {
    context.RemoveRange(users); // this throws exception "The property 'UserID' on entity type 'Contact' has a temporary value"
    throw;
}


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