我正在使用以下ComplexTypes:
CityStateRegion.cs
public class CityStateRegion
{
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
public string Country { get; set; }
public string Airport { get; set; }
}
Address.cs
public class Address
{
public string AddressOne { get; set; }
public string AddressTwo { get; set; }
public CityStateRegion Area { get; set; }
}
我将这些分成两个独立的复杂类型的原因是b / c我想在某些实体中使用CityStateRegion
而在其他实体中需要完整地址...
public class Person
{
public Guid PersonId { get; private set; }
public CityStateRegion Location { get; set; }
}
与
public class Company
{
public Guid CompanyId { get; private set; }
public Address Address { get; set; }
}
我发现我必须在FluentApi中执行以下操作,以确保所有与Area
相关的字段都符合要求:
//Address ComplexType Configurations
modelBuilder.ComplexType<Address>()
.Property(a => a.AddressOne).HasMaxLength(100).IsOptional();
modelBuilder.ComplexType<Address>()
.Property(a => a.AddressTwo).HasMaxLength(100).IsOptional();
modelBuilder.ComplexType<Address>()
.Property(a => a.Area.State).IsRequired(); //CityStateRegion
modelBuilder.ComplexType<Address>()
.Property(a => a.Area.City).IsRequired(); //CityStateRegion
//... more props omitted
然后我想知道......以下是什么区别?
modelBuilder.Entity<Company>()
.Property(c => c.Address.Area.City).HasMaxLength(50).Required();
VS
modelBuilder.ComplexType<Address>()
.Property(a => a.Area.City).HasMaxLength(50).Required();
VS
modelBuilder.ComplexType<CityStateRegion>()
.Property(a => a.City).HasMaxLength(50).IsOptional();
问题是 - 有时它应该是必需的,有时则不应该。
回顾一下:
ComplexType
是典型的吗? ComplexType
属性根据哪个实体使用它们而发生变化,那么设置position: absolute;
left: 50%;
属性的标准方法是什么?我不相信我在选项上有很多选择,但想看看其他人如何设置它。一个属性设置是否胜过下一个?我在MSDN上找到答案时遇到了麻烦,并且不想尝试三种不同的迁移。