Entity Framework 6.0 Code first - getting duplicated items in a simple query when filtering by primary key

.net c# ef-code-first entity-framework-6

Popular Answer

Should employFirstOrDefault in place ofSingleOrDefault because the Id column in your screen short has the same value. Consequently, you must do this.

Otherwise, you must verify that ID is a primary key. and 15 to zzz

because

FirstOrDefault()

is used when there are expected to be zero or more results in the input collection. If there are multiple results, the call returns the first one; if there are none, it returns the default value.

SingleOrDefault()

is used when there should be either zero or one result in the input collection, and the call only returns one result if there is exactly one result. Standard if no outcomes and an exception if several outcomes.

the following When should you use FirstOrDefault() instead of SingleOrDefault() when filtering criteria?

Updated:

The code initial model was created with the knowledge that you reverse-engineered the database while the Id column had not yet been set to IDENTITY.DatabaseGeneratedOption.None set for the entity's Id property.

When the column is changed to IDENTITY, EF creates an insert statement with the Id set as a result, which is invalid.

That settings must be manually corrected by either changing it toDatabaseGeneratedOption.Identity Considering that it is the default value for integer fields, you could just remove it entirely.

You must alter.

this.HasKey(e => e.Id);
   Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

Added details

Migrations in EF Code First 5.0.rc do not update the Identity property.

Database creation in Entity Framework 5 does not happen first

Edit : Please deleted your old table values and re generate it or remove same primary key rows. You should need to regenerate your CateringServices table values :), if the problems not cleared yet.

2
5/23/2017 12:33:29 PM


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