EntityHistory对于拥有类型而引用其他实体失败

问题描述 投票:0回答:1

这是我的模型:

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”值并且保存操作被终止:

enter image description here enter image description here

是否有任何解决方法或我们不能将引用存储在自有实体类型中?

c# asp.net-core aspnetboilerplate abp
1个回答
0
投票

如果有人想要一个解决方法,则需要覆盖所拥有实体的默认外键,以便我们传递始终位于外键集合中第一个位置的属性名称:

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());
}

结果是:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.