繁体   English   中英

Entity Framework Core 2.1 - 自有类型和嵌套值对象

[英]Entity Framework Core 2.1 - owned types and nested value objects

我正在学习 DDD,我目前正在学习的教程是使用 NHibernate 实现的,但由于我缺乏使用它的经验,我决定使用 EF Core 2.1 完成本课程。

但是,我目前有点坚持以下几点:我有三个类Customer ,它是一个实体和两个值对象( CustomerStatus和它内部的值对象ExpirationDate ) - 像这样:

public class Customer : Entity
{
    //... constructor, other properties and behavior omitted for the sake of simplicity
    public CustomerStatus Status { get; set; }
}

public class CustomerStatus : ValueObject<CustomerStatus>
{
    // customer status is enum containing two values (Regular,Advanced)
    public CustomerStatusType Type { get; }
    public ExpirationDate ExpirationDate { get; }
}

public class ExpirationDate : ValueObject<ExpirationDate>
{
    //... constructor, other properties and behavior omitted for the sake of simplicity
    public DateTime? Date { get; private set; }
}

当我尝试在我的DbContext中执行以下操作时:

modelBuilder.Entity<Customer>(table =>
{      
   table.OwnsOne(x => x.Status,
     name =>
     {
        name.Property(x => x.ExpirationDate.Date).HasColumnName("StatusExpirationDate");
        name.Property(x => x.Type).HasColumnName("Status");
     });
});

我收到以下错误:

表达式“x => x.ExpirationDate.Date”不是有效的属性表达式。 该表达式应表示一个简单的属性访问:'t => t.MyProperty'。
参数名称:propertyAccessExpression'

除此之外,我还尝试过以下操作:

table.OwnsOne(x => x.Status.ExpirationDate,
      name =>
      {
         name.Property(x => x.Date).HasColumnName("StatusExpirationDate");
      });
 table.OwnsOne(x => x.Status,
      name =>
      {
          name.Property(x => x.Type).HasColumnName("Status");
      });

但它也会导致:

表达式“x => x.Status.ExpirationDate”不是有效的属性表达式。 该表达式应表示一个简单的属性访问:'t => t.MyProperty'。

我也试过:

modelBuilder.Entity<Customer>()
                    .OwnsOne(p => p.Status, 
              cb => cb.OwnsOne(c => c.ExpirationDate));

但也没有运气......无论如何,任何帮助将不胜感激,如果可能的话,如果有人能解释为什么我的尝试都不起作用,那将是非常好的? 提前致谢!

更新

首先按照 Ivan 的评论中所述进行操作后,我收到有关CustomerStatus类构造函数的错误,因此我添加了默认保护的构造函数。

之后我开始收到错误:

实体类型“CustomerStatus”的字段“k__BackingField”是只读的,因此无法设置。

如果有帮助,这是我的CustomerStatus类的内部:

public class CustomerStatus : ValueObject<CustomerStatus>
{
    public CustomerStatusType Type { get; }
    public ExpirationDate ExpirationDate { get; }

    public static readonly CustomerStatus Regular =
        new CustomerStatus(CustomerStatusType.Regular, ExpirationDate.Infinite);
    public bool IsAdvanced => Type == CustomerStatusType.Advanced && !ExpirationDate.IsExpired;

    private CustomerStatus(CustomerStatusType type, ExpirationDate expirationDate)
    {
        Type = type;
        ExpirationDate = expirationDate;
    }

    protected CustomerStatus()
    {

    }
    public static CustomerStatus Create(CustomerStatusType type, ExpirationDate expirationDate)
    {
        return new CustomerStatus(type, expirationDate);
    }

    public CustomerStatus Promote()
    {
        return new CustomerStatus(CustomerStatusType.Advanced, ExpirationDate.Create(DateTime.UtcNow.AddYears(1)).Value);
    }

    protected override bool EqualsCore(CustomerStatus other)
    {
        return Type == other.Type && ExpirationDate == other.ExpirationDate;

    }

    protected override int GetHashCodeCore()
    {
        return Type.GetHashCode() ^ ExpirationDate.GetHashCode();
    }
}

更新

所要做的只是在CustomerStatus类内的TypeExpirationDate属性上添加私有设置器,并结合 Ivan 的回答,它就像一个魅力。 非常感谢!

您的尝试不起作用,因为拥有的类型只能通过其所有者实体进行配置,更具体地说,通过OwnsOne方法返回的自己的构建器或作为所有者实体的OwnsOne方法的Action<T>参数的参数提供建设者。

所以配置应该是这样的(注意嵌套的OwnsOne ):

modelBuilder.Entity<Customer>(customer =>
{      
    customer.OwnsOne(e => e.Status, status =>
    {
        status.Property(e => e.Type).HasColumnName("Status");
        status.OwnsOne(e => e.ExpirationDate, expirationDate =>
        {
            expirationDate.Property(e => e.Date).HasColumnName("StatusExpirationDate");
        });
    });
});

暂无
暂无

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

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