开发者

Entity Framework 4 & Code First CTP 5 - Missing Key

Can someone make sense of this error?

One or more validation errors were detected during model generation:

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

I have this entity defined:

public class Address
{
    [Key]
    public int ID;

    [Required]
    [MinLength(1)]
    [MaxLength(200)]
    public string Address1 { get; set; }

    [MinLength(1)]
    [MaxLength(200)]
    public string Address2 { get; set; }

    [Required]
    [MinLength(1)]
    [MaxLength(10)]
    public string Zip { get; set; }

    [MinLength(1)]
    [MaxLength(100)]
    public string Province { get; set; }

    public virtual US_State State { get; set; }

    [Required]
    public virtual Country Country { get; set; }
}

My question is:开发者_如何转开发 how does the error make any sense for a class that both has a Key attribute data annotation as well as the conventional ID name for its PK.

I would think this class satisfies all rules needed for a meaningful entity to be generated from it.


Like Craig mentioned, making ID a property will solve your problem.

public int ID { get; set; }

Besides, you don't need the [Key] attribute on ID, it will be recognized as object identifier (i.e. Primary Key) by code first based on conventions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜