EF Core跟踪不可变值对象

时间:2018-01-11 16:05:54

标签: c# domain-driven-design ddd-repositories ef-core-2.0

在我的应用程序中,我遵循DDD原则,因此我有一些ValueObject像页脚设置

public class FooterSettings : ValueObjectBase<FooterSettings>
{
    public bool ShowAddress { get; private set; }
    public bool ShowPageCount { get; private set; }
    public bool ShowAppVersion { get; private set; }

    private FooterSettings() { }

    public FooterSettings (bool showAddress, bool showPageCount, bool showAppVersion)
    {
        this.ShowAddress = showAddress;
        this.ShowPageCount = showPageCount;
        this.ShowAppVersion = showAppVersion ;
    }
}

和我的DBcontext模型创建映射

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Domain.Page>()
            .OwnsOne(x => x.FooterSettings); 
}

现在,每当我更改FooterSettings并尝试将新值对象设置为Page.FooterSettings时,我都会收到以下异常

The instance of entity type 'Page.FooterSettings#FooterSettings' cannot be tracked because another instance with the key value 'PageId:8' is already being tracked. When replacing owned entities modify the properties without changing the instance or detach the previous owned entity entry first.

0 个答案:

没有答案