1

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.

Community
  • 1
  • 1
Khalil Liraqui
  • 385
  • 1
  • 5
  • 21

1 Answers1

1

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.

sachin
  • 2,341
  • 12
  • 24
  • they are in the same project i've verified that by the "f12" trick and i got two links for the two partial classes. – Khalil Liraqui Aug 03 '16 at 14:04
  • 1
    Did you try adding the `Key` annotation on the primary class definition itself? Does that work? – sachin Aug 03 '16 at 14:08
  • @sachin While adding the attributes to the primary class should work, the class being generated means the edits would be overwritten every time the class was generated. Hence the need for a separate metadata class for the manually-applied attributes. – Suncat2000 Oct 20 '20 at 13:02