Entity framework core with value objects

aggregates c# domain-driven-design entity-framework-core

Question

I'm are running in an problem by using aggregates and value object in combination with 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

The entity configuration for generating the database is done this way and generates the database correctly conform DDD principals:

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

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. 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'

How to solve this without getting loose of value objects and changing them to entities by adding an identifier?

All help will be appreciated

1
0
3/13/2018 11:03:33 AM

Popular Answer

This DDD value-object scenario is not supported by now and will be addresses from version 2.1 of the EF Core.

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

0
3/13/2018 10:03:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow