简体   繁体   English

具有价值对象的实体框架核心

[英]Entity framework core with value objects

I'm are running in an problem by using aggregates and value object in combination with EF7. 通过将聚合和值对象与EF7结合使用,我遇到了问题。

This is the aggregate: 这是合计:

public class MasterAgreement
{
    private string _name;

    public Guid Key { get; set; }
    public MasterAgreementRaise RegularRaise { get; private set; }
    public MasterAgreementRaise MarketRaise { get; private set; }

    protected MasterAgreement()
    { }

    public MasterAgreement(Guid key)
    {
        Key = key;
    }

    public MasterAgreement(Guid key, string name, MasterAgreementRaise regularRaise, MasterAgreementRaise marketRaise)
    {
        Key = key;
        _name = name;
        RegularRaise = regularRaise ?? throw new DomainException("Regular raise is missing");
        MarketRaise = marketRaise ?? throw new DomainException("Market raise is missing");
    }

    public void Update(MasterAgreement masterAgreement)
    {
        _name = masterAgreement._name;
        RegularRaise = masterAgreement.RegularRaise;
        MarketRaise = masterAgreement.MarketRaise;
    }
   }

This is the value object: 这是值对象:

public class MasterAgreementRaise : ValueObject
{
    public int RaiseType { get; private set; }
    public int ConditionType { get; private set; }
    public decimal? Value { get; private set; }

    private MasterAgreementRaise() { }

    public MasterAgreementRaise(int raiseType, int conditionType, decimal? value)
    {
        RaiseType = raiseType;
        ConditionType = conditionType;
        Value = value;
    }

    protected override IEnumerable<object> GetAtomicValues()
    {
        // Using a yield return statement to return each element one at a time
        yield return RaiseType;
        yield return ConditionType;
        yield return Value;
    }
}

So the MasterAgreement contains 2 public MasterAgreementRaise fields 因此,MasterAgreement包含2个公共MasterAgreementRaise字段

The entity configuration for generating the database is done this way and generates the database correctly conform DDD principals: 用于生成数据库的实体配置是通过这种方式完成的,并且生成的数据库正确符合DDD主体:

public class MasterAgreementTypeConfiguration : IEntityTypeConfiguration<MasterAgreement>
{
    public void Configure(EntityTypeBuilder<MasterAgreement> builder)
    {
        builder.HasKey(item => item.Key);

        builder.Property<Guid>("Key");
        builder.Property<string>("Name").IsRequired().HasMaxLength(250);

        builder.OwnsOne(item => item.RegularRaise);
        builder.OwnsOne(item => item.MarketRaise);
    }
}

When trying to update or delete a master agreement from the database EF7 can't handle this 尝试从数据库中更新或删除主协议时,EF7无法处理此问题

The error during update is: The instance of entity type 'MasterAgreement.RegularRaise#MasterAgreementRaise' cannot be tracked because another instance with the same key value for {'MasterAgreementKey'} is already being tracked. 更新期间的错误是:无法跟踪实体类型'MasterAgreement.RegularRaise#MasterAgreementRaise'的实例,因为已经跟踪了另一个具有相同'{'MasterAgreementKey'}键值的实例。 When replacing owned entities modify the properties without changing the instance or detach the previous owned entity entry first. 替换拥有的实体时,请在不更改实例的情况下修改属性,或者先分离先前的拥有实体条目。

And during delete is: The entity of 'MasterAgreement' is sharing the table 'MasterAgreement' with MasterAgreement.RegularRaise#MasterAgreementRaise', but there is no entity of this type with the same key value that has been marked as 'Deleted' 并且在删除过程中是:'MasterAgreement'的实体与MasterAgreement.RegularRaise#MasterAgreementRaise'共享表'MasterAgreement',但是没有这种类型的具有相同键值的实体被标记为'Deleted'

How to solve this without getting loose of value objects and changing them to entities by adding an identifier? 如何解决这个问题而又不失去价值对象并通过添加标识符将它们更改为实体?

All help will be appreciated 所有帮助将不胜感激

This DDD value-object scenario is not supported by now and will be addresses from version 2.1 of the EF Core. 此DDD值对象方案目前尚不支持,它将是EF Core 2.1版中的地址。

You can follow here the same issue that was resolved with just a better expcetion message and the final resolution for version 2.1 in this issue 您可以在此处关注已解决的同一问题 ,只是获得了更好的过期消息以及此问题中版本2.1的最终解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM