EF 6:另一种复杂类型中的嵌套复杂类型 [英] EF 6 : Nested Complex type in another complex type

查看:106
本文介绍了EF 6:另一种复杂类型中的嵌套复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个称为汽车的实体。

Suppose that I have a Entity called "car".

我使用复杂的类型来定义引擎部分。

I use a complex type to define the "Engine" part.

[TableName("T_CAR")]
public sealed class Car:IEngine
{
  ...
  public EngineType EngineType{get;set;}

}

[ComplexType]
public sealed class Engine:IEngine
{

  public Engin( )
  {
    this.EnginType = new EngineType( );
  }

  public EngineType EngineType{get;set;}

  // other properties ...

}

[ComplexType]
public sealed class EngineType:IEngineType
{
  ...     
}

运行此代码时,我收到来自EF 6的错误消息:

When I run this code, I have a error message from EF 6 :

System.InvalidOperationException:类型'引擎已配置为实体类型。

System.InvalidOperationException: The type 'Engine' has already been configured as an entity type. It cannot be reconfigured as a complex type.

如果我删除了EngineType的定义,它将起作用...

And if I remove the definition of EngineType, it works ...

是否可以在另一个复杂类型中添加嵌套的复杂类型?

Is there a way to add a nested complex type in another complex type ?

推荐答案

编辑:这已经改变,EF6现在支持嵌套的复杂类型,只记得将每个复杂类型都设置为一个。

Edit: This has changed, EF6 now supports nested complex types, just remember to set each one as a complex type.

public class EngineConfiguration : ComplexTypeConfiguration<Engine>
{
}
public class EngineTypeConfiguration : ComplexTypeConfiguration<EngineType>
{
}

然后在您的上下文中:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new EngineConfiguration());
    modelBuilder.Configurations.Add(new EngineTypeConfiguration());
}






原始答案:不, ComplexTypes只能包含基本类型。我首先在朱莉·勒曼斯(Julie Lermans)的书
先编程实体框架代码中阅读了此书,
我不认为EF6中已取消此限制。


Original answer: No, ComplexTypes may only contain primitive types. I first read this in Julie Lermans book Programming entity framework code first, I don't believe this restriction has been lifted in EF6.

这篇关于EF 6:另一种复杂类型中的嵌套复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆