这是我的模型:
public class Investment : FullAuditedEntity<Guid>
{
public string some_property { get; set; }
public Address Address { get; set; }
}
public class Address : ValueObject<Address>
{
[ForeignKey("CountryId")]
public Country Country { get; set; }
[Required]
public int CountryId { get; set; }
[ForeignKey("StateId")]
public State State { get; set; }
public string StateId { get; set; }
[ForeignKey("DistrictId")]
public District District { get; set; }
public string DistrictId { get; set; }
[ForeignKey("CommuneId")]
public Commune Commune { get; set; }
public string CommuneId { get; set; }
[Required]
public string City { get; set; }
public string Street { get; set; }
}
当我尝试创建新投资并保存到数据库时,ABP会尝试确定是否应该在历史表中存储实体更改,但在尝试识别所拥有实体(地址)的所有者(投资)时会崩溃。这是因为ABP总是采用第一个外键(假设它与所有者实体的关系),但在我的情况下,第一个外键与某个其他实体的关系,因此没有“PrincipalToDependent”值并且保存操作被终止:
是否有任何解决方法或我们不能将引用存储在自有实体类型中?
如果有人想要一个解决方法,则需要覆盖所拥有实体的默认外键,以便我们传递始终位于外键集合中第一个位置的属性名称:
public class InvestmentConfiguration : IEntityTypeConfiguration<Investment>
{
public void Configure(EntityTypeBuilder<Investment> configuration)
{
configuration.OwnsOne(typeof(Address), "Address", buildAction => buildAction.HasForeignKey("AInvestmentId"));
}
}
然后在DBContext类中:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new InvestmentConfiguration());
}