2012-07-29 31 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; } 
}