EfCore 2.2拥有的类型选择实体基础属性

时间:2019-06-19 17:45:49

标签: .net-core entity-framework-core

我有一个带有此签名的名为Money的ValueObject:

public class Money : ValueObject
{
    public decimal Amount { get; private set; }
    public Currency Currency { get; private set; }

    private Money(decimal amount, Currency currency = Currency.Nigerian_Naira)
    {
        Guard.Against.Zero(amount, nameof(amount));
        Amount = amount;
        Currency = currency;
    }

    private Money(){}

    public static Money Create(decimal amount, Currency currency = Currency.Nigerian_Naira)
    {
        return new Money(amount, currency);
    }

    public bool IsEmpty()
    {
        return Amount == 0m && Currency == Currency.Nigerian_Naira;
    }
    protected override IEnumerable<object> GetAtomicValues()
    {
        yield return Amount;
        yield return Currency;
    }
}

并且我有一个拥有此值对象的实体,如下所示:

public class Account : BaseEntity
{

    public Account()
    {
        AccountId = Guid.NewGuid();
    }

    public Guid AccountId { get; set; }

    [Required]
    [StringLength(25)]
    public string AccountName { get; set; }

    [Required]
    public Money AccountBalance { get; set; }

    [Required]
    public bool IsActivated { get; set; }

    public List<Entity2> Entity2 { get; set; }

    public List<Entity1> Entity1 { get; set; }

}

我已经在Context中配置了所有权,如下所示:

modelBuilder.Entity<Account>()
                    .OwnsOne(c => c.AccountBalance);

这是我的基本实体,基本上带有如下所示的简单属性:

public abstract class BaseEntity
{
    public bool IsDeleted { get; set; }

    public DateTimeOffset CreatedDate { get; set; }

    public DateTimeOffset UpdatedDate { get; set; }

}

现在,如果我想在单元测试中将更改保存到内存数据库中,则会出现错误提示:

Error Message:
System.InvalidOperationException : The property 'UpdatedDate' on entity type 
'CashAccount.AccountBalance#Money' could not be found. Ensure that the 
property exists and has been included in the model.

updatedDate属性来自所有实体都继承自的基本实体类型。我一直在梳理efcore文档,但没有发现任何有关这种怪异行为的线索。

我基本上是想将Account保存到数据库中,这给了我这个错误,说Money没有属性UpdatedDate,该属性应该属于实体。 EF6复杂类型从来都不是问题。

0 个答案:

没有答案