I have a existing DB and I use the approach “DB First” with Entity Framework 4.1. Unfortunately, the DB is using some legacy naming conventions so that the columns with primary keys often use various names such as “occID”. Later on during validation the EntityFramework throws an exception when validating the entity model saying that
System.Data.Edm.EdmEntityType: : EntityType ‘Occupancy’ has no key defined. Define the key for this EntityType.
This entity has a unique key (and it is also marked as EntityKey in the Edmx model), but the model validator keeps complaining. The only solution I found is to sign the property in the generated DbContext class with the data annotation [Key] so that EF knows which property is a key.
Are there other ways to do that? I suppose that EF is checking some coding conventions to recognize which property is a Key. Since this is not a code-first approach, I am not allowed to change the generated code manually as the changes get lost everytime I change something in the model.
This is the corresponding part of the EDMX file:
<EntityType Name="occupancy">
<Key>
<PropertyRef Name="occID" />
</Key>
<Property Name="occID" Type="int" Nullable="false"
StoreGeneratedPattern="Identity" />
<Property Name="occPDM" Type="int" />
<Property Name="occDatePDM" Type="datetime" />
<Property Name="occDatePC" Type="datetime" />
<Property Name="occOccupancy" Type="int" />
<Property Name="occSityControlID" Type="bigint"
/>
<Property Name="occSityControlLine" Type="int"
/>
<Property Name="occPDM_Id" Type="bigint" />
<Property Name="occServiceCar" Type="int" />
<Property Name="occMoneyCar" Type="int" />
<Property Name="occPdmParentId" Type="int" />
</EntityType>
Well, I solved this problem by removing all generated classes and generating them completely fom the beginning. Now the Entity Model contains corresponding keys and I do not need to modify the model manually.