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 '的构造函数。以下参数无法绑定到实体的属性:“地址”。
| 归档时间: |
|
| 查看次数: |
2113 次 |
| 最近记录: |