我是 EF 的新手,我有这样的问题:我有四个实体,它们描述了地址的元素:
public partial class Region : BaseEntity
{
private ICollection<RegionCity> _regionCities;
public string Code { get; set; }
public string Name { get; set; }
public int Timezone { get; set; }
public virtual ICollection<RegionCity> RegionCities
{
get
{
return _regionCities ?? (_regionCities = new List<RegionCity>());
}
protected set { _regionCities = value; }
}
}
public partial class RegionCity : BaseEntity
{
private ICollection<CityStreet> _cityStreets;
public int RegionId { get; set; }
public string CityName { get; set; }
public virtual Region Region { get; set; }
public virtual ICollection<CityStreet> CityStreets
{
get { return _cityStreets ?? (_cityStreets = new List<CityStreet>()); }
protected set { _cityStreets = value; }
}
}
public partial class CityStreet : BaseEntity
{
public int CityId { get; set; }
public string StreetName { get; set; }
public virtual RegionCity City { get; set; }
}
public partial class FullAddress : BaseEntity
{
public int CityStreetId { get; set; }
public string HouseNum { get; set; }
public string OfficeNum { get; set; }
public virtual CityStreet Street { get; set; }
}
我有映射类:
public class RegionMap : EntityTypeConfiguration<Region>
{
public RegionMap()
{
this.ToTable("Region");
this.HasKey(r => r.Id);
this.Property(r => r.Name).HasMaxLength(64);
}
}
public class RegionCityMap : EntityTypeConfiguration<RegionCity>
{
public RegionCityMap()
{
this.ToTable("RegionCity");
this.HasKey(rc => rc.Id);
this.HasRequired(rc => rc.Region)
.WithMany(r => r.RegionCities)
.HasForeignKey(rc => rc.RegionId)
.WillCascadeOnDelete(false);
}
}
public class CityStreetMap : EntityTypeConfiguration<CityStreet>
{
public CityStreetMap()
{
this.ToTable("CityStreet");
this.HasKey(cs => cs.Id);
this.HasRequired(cs => cs.City)
.WithMany(c => c.CityStreets)
.HasForeignKey(cs => cs.CityId)
.WillCascadeOnDelete(false);
}
}
public class FullAddressMap : EntityTypeConfiguration<FullAddress>
{
public FullAddressMap()
{
this.ToTable("FullAddress");
this.HasKey(fu => fu.Id);
this.Property(fu => fu.HouseNum).HasMaxLength(16);
this.HasRequired(fu => fu.Street)
.WithMany()
.HasForeignKey(fu => fu.CityStreetId)
.WillCascadeOnDelete(false);
}
}
我如何编写映射类,如果我将更改类 FullAddress 如下所示,以便它证明其名称是合理的:
public partial class FullAddress : BaseEntity
{
public int CityStreetId { get; set; }
public string HouseNum { get; set; }
public virtual Region Region { get; set; }
public virtual RegionCity City { get; set; }
public virtual CityStreet Street { get; set; }
}
我可以通过 CityStreet 映射 RegionCity,然后通过 RegionCity 映射 Region?