问题描述
我在我的项目中使用使用值对象的实体,我在文档中读到如果你想使用值对象,你必须使用拥有的类型。 当我用它的存储库保存我的实体时,这个工作完美,但是当我尝试加载数据时,这不起作用,它只加载我的 ID 值对象,而不加载其他对象
这是我的背景
public class WikiContext : DbContext
{
private string _sConnection;
#region Constructs
public WikiContext()
{
}
public WikiContext(string sConnection)
{
_sConnection = sConnection;
}
#endregion
#region Entities
public DbSet<Customer> Customers{ get; set; }
#endregion
#region Mapping
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UsesqlServer(_sConnection);
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new CustomerConfiguration());
base.OnModelCreating(modelBuilder);
}
#endregion
}
这是我的客户配置:
public class CustomerConfiguration : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
// https://stackoverflow.com/questions/49607375/ef-core-dbcontext-map-custom-type-as-primary-key
// http://thedatafarm.com/data-access/entity-framework-private-constructors-and-private-setters/
builder.ToTable("Customers");
builder.HasKey(customer=> new { customer.Id });
builder
.Property<CustomerId>("Id")
.HasConversion(id => id.Value,guid => new CustomerId(guid));
// https://www.edgesidesolutions.com/ddd-value-objects-with-entity-framework-core/
builder.OwnsOne(c => c.Name,a =>
{
a.Property(p => p.Value).HasColumnName("Name");
a.WithOwner();
});
builder.OwnsOne(c => c.StudioId,a =>
{
a.Property(p => p.Value).HasColumnName("StudioId").HasColumnType("uniqueidentifier");
a.WithOwner();
});
}
}
这是我的客户存储库方法
public Customer[] Search()
{
var customers = Context.Customers.Select(customer=> customer);
return customers.ToArray();
}
我可以用下一个模型配置做一个解决方法,但我仍然对这种方式有疑问
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(builder =>
{
builder.ToTable("Customers");
builder.HasKey(customer => new { customer.Id });
builder
.Property<CustomerId>("Id")
.HasConversion(id => id.Value,guid => new CustomerId(guid));
builder
.Property<CustomerName>("Name")
.HasColumnName("Name")
.HasConversion(name => name.Value,value => new CustomerName(value));
builder
.Property<StudioId>("StudioId")
.HasColumnName("StudioId")
.HasConversion(p => p.Value,value => new StudioId(value));
});
base.OnModelCreating(modelBuilder);
}
#endregion
这是客户实体
public class Customer: IAggregateRoot
{
#region Constructors
private Customer()
{
}
public Customer(CustomerId oId,CustomerName oCustomerName,StudioId oStudioId)
{
Id = oId;
Name = oCustomerName;
StudioId = oStudioId;
}
#endregion
#region Public Methods
public static Customer Create(CustomerId oId,CustomerName oName,StudioId oStudioId)
{
var oCustomer = new Customer(oId,oName,oStudioId);
//Todo: implement event sourcing here
return oCustomer;
}
#endregion
#region Property
public CustomerId Id { get; set; }
public CustomerName Name { get; set; }
public StudioId StudioId { get; set; }
#endregion
}
public class CustomerName
{
public string Value { get; set; }
#region Constructors
private CustomerName()
{
}
public CustomerName(string sName)
{
Value = sName;
}
#endregion
}
我不知道问题出在哪里,我尝试了几种模型配置,但我仍然可以理解为什么这不起作用。
我将 EntityFrameworkCore 5.0.1 与 sql Server 和 .Net Core 3.1 一起使用
解决方法
这是在官方 EFCore 的 github 存储库中回答的,这里是答案的链接 answer