我有以下内容:
映射:
modelBuilder.Entity<CostCentreCopy>().Property(p => p.CostCentreId).HasColumnName("ID_COST_CENTRE").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<CostCentreCopy>().HasRequired(e => e.Address).WithRequiredDependent(a => a.CostCentreCopy);
modelBuilder.Entity<CostCentreAddress>().HasRequired(e => e.CostCentreCopy).WithRequiredPrincipal(a => a.Address);
modelBuilder.Entity<CostCentreAddress>().Property(p => p.CostCentreId).HasColumnName("ID_COST_CENTRE").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<CostCentreAddress>().Property(p => p.Address1).HasColumnName("TX_ADDRESS_1").IsRequired();
modelBuilder.Entity<CostCentreAddress>().Property(p => p.Address2).HasColumnName("TX_ADDRESS_2").IsRequired();
modelBuilder.Entity<CostCentreAddress>().Property(p => p.City).HasColumnName("TX_CITY").IsRequired();
modelBuilder.Entity<CostCentreAddress>().Property(p => p.State).HasColumnName("TX_STATE").IsRequired();
modelBuilder.Entity<CostCentreAddress>().Property(p => p.Country).HasColumnName("TX_COUNTRY").IsRequired();
modelBuilder.Entity<CostCentreAddress>().Property(p => p.Postcode).HasColumnName("TX_POST_CODE").IsRequired();
modelBuilder.Entity<CostCentreAddress>().ToTable("MIC_COST_CENTRE_COPY");
modelBuilder.Entity<CostCentreCopy>().ToTable("MIC_COST_CENTRE_COPY");
型号:
public class CostCentreCopy
{
[Key]
public int CostCentreId { get; set; }
public virtual CostCentreAddress Address { get; set; }
}
public class CostCentreAddress
{
[Key]
public int CostCentreId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string Postcode { get; set; }
public string State { get; set; }
public virtual CostCentreCopy CostCentreCopy { get; set; }
}
有了这个,我可以使用地址详细信息保存所有CostCentreCopy。 但是,当我通过Id获得CostCentreCopy时,我只获得CostCentreCopy而不是地址。
该表是一个带有标识列的平面表。
有没有办法使用EF在主表作为标识的表上使用表拆分来创建,返回,更新和删除。
由于
答案 0 :(得分:0)
在您的关系船中定义虚拟时
public virtual CostCentreAddress Address { get; set; }
它被评估为延迟加载的候选者,因此您可以明确说出.Include(c c =&gt; Address)或删除虚拟关键字。
如果这有助于别忘了将其标记为答案:)