【问题标题】:Configure value object with inheritance relationship in EF Core 6 Fluent API在 EF Core 6 Fluent API 中配置具有继承关系的值对象
【发布时间】:2023-01-19 06:13:45
【问题描述】:

我正在尝试在我的一个项目中实现 DDD,我正在努力在 ef-core 6 fluent api 中配置值对象。

我的值 Object 是一个抽象类型 Schedule ,它具有具体类型,例如 Daily、Monthly 等。

public abstract class Schedule : ValueObject
{
 public DateOnly StartFrom { get; init; }

}

public class Monthly : Schedule
{
  public List<int> DaysOfMonth { get; private set; }
}

// Entity which owns the Value Object :-

public class SubscribedItem : Entity
{
   public Schedule Schedule { get; private set; }
}

public class SubscribedItemEntityTypeConfiguration : 
       IEntityTypeConfiguration<SubscribedItem>
{
    public void Configure(EntityTypeBuilder<SubscribedItem> builder)
   {
    builder.OwnsOne(p => p.Schedule, pp =>
    {
        pp.Property(ppp => ppp.StartFrom).IsRequired();
      // How to configure concrete type Monthly value object.
    });
   }
 }

如何配置具体类型值对象。 即我希望在每月计划类型 DaysOfMonth 中进行序列化和反序列化,同时从数据库中保存和检索

【问题讨论】:

    标签: inheritance domain-driven-design value-objects ef-core-6.0


    【解决方案1】:

    在值对象导航属性的配置中使用 pp.OwnsOne。但是,您还需要为 DaysOfMonth 属性设置一个 value conversions

    【讨论】:

      猜你喜欢
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 2012-01-20
      • 2014-07-22
      • 2021-10-16
      • 1970-01-01
      • 2022-10-17
      相关资源
      最近更新 更多