1

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; }
}
4

1 に答える 1

7

既定では、Entity Framework は、Id というキー プロパティがモデル クラスに存在すると想定します。キー プロパティは CustomerID と呼ばれるため、Entity Framework はそれを見つけることができません。

キー プロパティの名前を CustomerID から Id に変更するか、CustomerID プロパティをKey属性で装飾します。

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; }
}
于 2012-07-29T00:43:29.183 に答える