2013-11-04 107 views
0

我有两个类:地址和城市。我想在地址类需要城市属性,但是当我添加property(p => p.City).IsRequired()以流利的API,我得到错误市必须非空值类型,但是当我装点市物业与[必需]注释一切正常。

那么如何用流利的API做,为什么property(p => p.Street).IsRequired()作品串 - 串isn'e非空值类型EF 6流利的API IsRequired属性

public class Address 
    { 
     public int AddressId { get; private set; } 
     public string Street { get; internal set; } 
     [Required] 
     public City City { get; internal set; }    
    } 

public class CIty 
{ 
     public int CityId {get; private set; } 
     public string Name {get; internal set;} 
} 

回答

4

为了指定关系的基数,你需要使用HasRequired方法,而不是 - 所述Property方法仅用于标量属性。

modelBuilder.Entity<Address>().HasRequired(a => a.City);