Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edmx need Primary_Key?

Its a problem While Creating Edmx in .net that on creating Edmx of DataBase only those Tables and views are added who had a primary key.
This problem resolved easily but just making a column primary key in Table or View but i didn't got the actual reason why it is necessary??
Can anyone please explain the reason behind it?

like image 257
Amit Bisht Avatar asked Dec 18 '13 10:12

Amit Bisht


People also ask

Does Entity Framework require a primary key?

If not explicitly specified, EF core checks if there is a property with name “ Id ” or {EntityName}Id . If there is such property, then it is by default marked as primary key for the entity. If there is no such property, then EF core throws an error: The entity type '{EntityName}' requires a primary key to be defined.

How do I regenerate EDMX?

There is no automatically refresh the EDMX (it will be nice from MS if they implement that at some point) and the best and most accurate way to refresh the EDMX is by deleting all tables in the Diagram and then deleting all complex types etc. in the Model Browser.

Can we use EDMX in .NET core?

EF Core does not support the EDMX file format for models.


1 Answers

Entity Framework needs a primary key to properly identify a piece of data as unique in a particular set of data. It comes down to how it looks up entities internally and giving you the best performance possible.

For example, one of the features of Entity Framework is Change Tracking, which keeps a collection of Added, Deleted, Modified, and Unchanged entities in a series of collections defined as Dictionary<EntityKey, EntityEntry>. To be able to effectively handle those collections, it needs a key to perform the necessary CRUD operations to those collections in a timely manner, and as a result, it needs that key.

Update:

There is also a collection for keyless objects as well, but it's defined as Dictionary<object, EntityEntry> which has horrible performance for lookups as the key would need to be cast (or unboxed if the key is a value type) for it to be usable.

like image 76
William Holroyd Avatar answered Sep 23 '22 21:09

William Holroyd