2

Entity

public class Region
{
    [Key]
    public int ID;
    public string Name; 
    public string Description;
}

Model

public class RegionModel
{   [Key]
    public int ID { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }
}

Errors

System.Data.Edm.EdmEntityType: : EntityType 'Region' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Regions� is based on type �Region� that has no keys defined.

tereško
  • 58,060
  • 25
  • 98
  • 150
Stalli
  • 374
  • 2
  • 5
  • 16

3 Answers3

6

Your class fields need to be changed to properties for EF to use the class correctly;

public class Region
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0
public class Region
{
    [Key]
    public int RegionId{get;set;}
    public string Name{get;set;} 
    public string Description{get;set;}
}

public class RegionModel
{   [Key]
    public int RegionModelId { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }
}

It works if you have it like ClassNameId.You can even remove [Key] attribute now.

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
0

Maybe a strange answer to your problem. But be sure that your project compiles first. i got the same errors when i had added the dataanotations without compiling the project.

I think the code is generated with some kind of reflection.

Christian van R
  • 617
  • 5
  • 6