2012-07-29 29 views
0

我得到这个错误在我的MVC应用程序:MVC无关键定义

One or more validation errors were detected during model generation:

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

我的客户模式是这样的:

public class CustomerModel 
{ 
    public string Name { get; set; } 
    public int CustomerID { get; set; } 
    public string Address { get; set; } 
} 

public class CustomerContext : DbContext 
{ 
    public DbSet<CustomerModel> Customer { get; set; } 
} 

回答

5

默认情况下,实体框架假定被称为Id的键属性存在于您的模型类中。你的key属性叫做CustomerID,所以Entity Framework找不到它。

无论是从客户ID的关键属性的名称更改为标识,或与重点属性装点CustomerID属性:

public class CustomerModel 
{ 
    public string Name { get; set; } 

    [Key] 
    public int CustomerID { get; set; } 

    public string Address { get; set; } 
} 

public class CustomerContext : DbContext 
{ 
    public DbSet<CustomerModel> Customer { get; set; } 
}