2
votes

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.

3

3 Answers

6
votes

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; }
}
0
votes
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.

0
votes

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.