C# EF Core can';t通过聚合构造函数传递值类型 问题
我需要初始化一个新的域实体,它通过构造函数接受一个基本值对象。域背后的持久性机制是由实体框架核心驱动的SQL server 我遇到的问题是,当efcore尝试创建数据库模式时,它无法将value对象参数与任何映射属性相关联 我错过了什么?任何帮助都将不胜感激 我的简化域模型 我的数据库模型配置C# EF Core can';t通过聚合构造函数传递值类型 问题,c#,entity-framework-core,C#,Entity Framework Core,我需要初始化一个新的域实体,它通过构造函数接受一个基本值对象。域背后的持久性机制是由实体框架核心驱动的SQL server 我遇到的问题是,当efcore尝试创建数据库模式时,它无法将value对象参数与任何映射属性相关联 我错过了什么?任何帮助都将不胜感激 我的简化域模型 我的数据库模型配置 模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder) { 基于模型创建(modelBuilder); 实体(生成器=> { HasKey(model=>model.Id); 属
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
实体(生成器=>
{
HasKey(model=>model.Id);
属性(model=>model.Title).IsRequired();
属性(model=>model.Description).IsRequired();
builder.Property(model=>model.GuidePrice).IsRequired();
builder.OwnsOne(model=>model.Address,Address=>
{
属性(model=>model.StreetAddress.IsRequired();
address.Property(model=>model.Town).IsRequired();
address.Property(model=>model.PostalCode.IsRequired();
});
});
}
结果
在创建数据库架构时,Entity Framework会引发以下异常:
系统。InvalidOperationException:“未找到实体类型的合适构造函数”PropertyList。无法将以下参数绑定到实体的属性:“地址””
我的技术堆栈
- 实体框架核心2.1.0-preview1-final
- ASP.NET核心2.0
PropertyListing(){…}
第三个链接指向EF Core中的一个未解决问题,因此,显然您遇到了当前的EF核心限制。@IvanStoev是的,我意识到问题是公开的,但我只是想知道我是否在问题讨论中遗漏了一些东西。@viveknuna建议的添加默认构造函数在我的情况下似乎有效。不知道为什么。
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();
});
});
}