【发布时间】:2019-03-13 21:24:30
【问题描述】:
在 DDD 中,将实体的标识作为值对象是一种常见的设计。
例子:
public class FooId : ValueObject
{
public int Id { get; private set; }
}
public class Foo
{
public FooId FooId { get; private set; }
}
在 EF6 中,我可以使用以下代码映射这些类型:
modelBuilder.ComplexType<SomeType>()
.Property(x => x.SomeProperty)
...
(见3 Reasons to Model Identity as A Value Object,IDDD)
编辑:IDDD in .Net
但是当我尝试映射 Foo 和 FooId 时,在迁移过程中出现以下错误
属性表达式“x => x.FooId.Id”无效。该表达式应表示一个属性:C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'。指定多个属性时使用匿名类型:C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'。
我的配置:
public class FooConfiguration : EntityTypeConfiguration<Foo>
{
public FooConfiguration()
{
HasKey(x => x.FooId.Id);
}
}
public class FooContext : EntityTypeConfiguration<Foo>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<FooId>();
modelBuilder.Configurations.Add(new FooConfiguration());
}
}
使用的包
- EntityFramework 6.2.0
- MySql.Data 6.10.7
- MySql.Data.Entity 6.10.7
【问题讨论】:
标签: mysql entity-framework-6 domain-driven-design entity-framework-migrations