我有两个班:地址和城市。我希望address类中需要城市属性,但是当我将property(p => p.City).IsRequired()添加到fluent api时,就会发现city必须非空值类型的错误,但是当我用所需的注释装饰City属性时,一切都可以工作。
那么,如何使用fluent api来实现它,以及为什么property(p => p.Street).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;}
}发布于 2013-11-04 17:46:11
为了指定关系的基数,您需要使用HasRequired方法-- Property方法仅用于标量属性。
modelBuilder.Entity<Address>().HasRequired(a => a.City);https://stackoverflow.com/questions/19772327
复制相似问题