3

有人能理解这个错误吗?

在模型生成期间检测到一个或多个验证错误:

System.Data.Edm.EdmEntityType: : EntityType 'Address' 没有定义键。定义此 EntityType 的键。System.Data.Edm.EdmEntitySet:EntityType:EntitySet 地址基于没有定义键的类型地址。

我定义了这个实体:

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

我的问题是:对于既具有 Key 属性数据注释以及其 PK 的常规 ID 名称的类,该错误有什么意义。

我认为这个类满足从它生成有意义的实体所需的所有规则。

4

1 回答 1

8

就像克雷格提到ID的那样,建造房产将解决您的问题。

public int ID { get; set; }

此外,您不需要[Key]on 属性ID,它会根据约定首先被代码识别为对象标识符(即主键)。

于 2011-02-08T21:38:26.790 回答