Khalil Liraqui
Khalil Liraqui

Reputation: 415

Adding annotation [key] in metaDataClass for an entity generated by EF database first

In my Odata web api I've generated my entities with Entity Framework Database first, then i want to add the annotation [key] to a field so i follow this indications : https://stackoverflow.com/a/16737247/6444829 to create a metadata class then a partial class and then add the annotation in the metadata class.

when i do so it doesn't consider the annotation and i got an error "The entity 'XXXX' does not have a key defined".

what am'i doing wrong, the annotation [Key] is it different from others ?

 public partial class personalInfo
{

    public int personId { get; set; }
    public int primaryPosition_id { get; set; }
    ...
}


[MetadataType(typeof(personalInfoMetaData))]
public partial class personalInfo
{

}


public class personalInfoMetaData
{
    [Key]
    public int personId { get; set; }

    [ForeignKey("positionInfo")]
    public int primaryPosition_id { get; set; }
}

ps : its a view that was retrieved from the database and transformed in an entity.

Upvotes: 1

Views: 593

Answers (1)

sachin
sachin

Reputation: 2361

Can you please make sure that both your partial class definitions are in the same namespace? For multiple partial definitions to be combined into one class, all those definitions have to share the same namespace.

These should be in the same project too. When you press F12(or choose go to defintion) while highlighting the class name, it should ask you which partial definition you want to go to. That way, you can verify that these two are linked.

Otherwise try declaring public int personId { get; set; } in your empty partial class and see if that raises an error.

Upvotes: 1

Related Questions