gpt4 book ai didi

mysql - 如何使用流畅的 api 在 EF6 中映射值对象标识?

转载 作者:行者123 更新时间:2023-11-29 05:52:40 24 4
gpt4 key购买 nike

在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 ObjectIDDD)

Edit: IDDD in .Net

但是当我尝试映射 Foo 和 FooId 时,在迁移过程中出现以下错误

The properties expression 'x => x.FooId.Id' is not valid. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: 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());
}
}

使用的包

  • Entity Framework 6.2.0
  • MySql.Data 6.10.7
  • MySql.Data.Entity 6.10.7

最佳答案

不幸的是,目前它在 EF 6.x 中是不可能的,您必须处理常规原语。尽管可以在 EF core 2.1 中使用 Value Conversions .

作为经典 .Net Framework 的替代方案,您可以尝试 NHibernate,因为它允许将值对象作为标识。从领域驱动设计的角度来看,NHibernate 看起来仍然比EF 更强大。

关于mysql - 如何使用流畅的 api 在 EF6 中映射值对象标识?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52715267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com