EF Core 无法通过聚合构造函数传递值类型

Jam*_*ler 5 c# entity-framework-core

问题

我需要初始化一个新的域实体,该实体通过其构造函数接受一个基本值对象。域背后的持久化机制是由 Entity Framework Core 驱动的 SQL server。

我遇到的问题是,当 EF Core 尝试创建数据库架构时,它无法将值对象参数与​​任何映射属性相关联。

我在这里缺少什么?任何帮助将非常感激。

我的简化域模型

public sealed class Address
{
    public string StreetAddress { get; }

    public string Town { get; }

    public string PostalCode { get; }

    internal Address(string streetAddress, string town, string postalCode)
    {
        ...
    }
}

public sealed class PropertyListing
{
    public Guid Id { get; }

    public string Title { get; }

    public string Description { get; }

    public Address Address { get; }

    public decimal GuidePrice { get; }

    internal PropertyListing(Guid id, string title, string description, Address address, decimal guidePrice)
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我的数据库模型配置

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<PropertyListing>(builder =>
        {
            builder.HasKey(model => model.Id);

            builder.Property(model => model.Title).IsRequired();

            builder.Property(model => model.Description).IsRequired();

            builder.Property(model => model.GuidePrice).IsRequired();

            builder.OwnsOne(model => model.Address, address =>
            {
                address.Property(model => model.StreetAddress).IsRequired();

                address.Property(model => model.Town).IsRequired();

                address.Property(model => model.PostalCode).IsRequired();
            });
        });
    }
Run Code Online (Sandbox Code Playgroud)

结果

当需要创建数据库架构时,Entity Framework 抛出以下异常:

System.InvalidOperationException:'找不到适合实体类型' PropertyListing '的构造函数。以下参数无法绑定到实体的属性:“地址”。

我的技术栈

  • 实体框架核心2.1.0-preview1-final
  • ASP.NET 核心2.0

参考资料