我需要初始化一个通过其构造函数接受基本值对象的新域实体。域后面的持久性机制是由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)
{
...
}
}
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();
});
});
}
当创建数据库模式时,Entity Framework会引发以下异常:
System.InvalidOperationException :'找不到实体类型' PropertyListing '的合适构造函数。以下参数无法绑定到实体的属性:'地址'。'