Error: An error occurred while preparing the command definition

Error: An error occurred while preparing the command definition

我在模型浏览器的 Function Imports 下有 2 个存储过程,比方说 ProcAProcB,每个都有自己的复杂类型,比方说 TypeAType2

TypeA 属性:

Int32 SomeId;
String PersonName;
String PersonAddress;

TypeB 属性

Int32 SomeId;
String PersonName;
String PersonAddress;
Int32 Age;
Int32 Height;

一切都很好,功能齐全,没有例外,除非我尝试将 TypeA 嵌套到 TypeB as TypeB 基本上是 的翻版TypeA 加上它有 2 个自己的附加属性。

TypeB 属性

Int32 Age;
Int32 Height;
TypeA TypeAObj; // nesting typeA here

ProcA 工作正常,但当我调用 ProcB 时它抛出异常:

Error: An error occurred while preparing the command definition. See the inner exception for details.

Message = "Nested ComplexType property 'TypeA' in the ReturnType 'TypeB' of the function 'ProcB' is not supported, please consider flattening the nested ComplexType property."

我是否必须使用扁平的复杂类型并且在我的情况下不可能嵌套?

我只是想减少代码行数并强制实现可重用性。

如果对此有任何意见,那就太好了。 如果我可以提供更多信息,请告诉我。

PS: DB.Context.cs 文件

中发生错误
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<TypeB>("ProcB", param1, param2);

谢谢

这是行不通的。从 EF 6.0 开始,它不受支持。

看看这个 link。 属性 提取了此错误背后问题的本地化字符串,它仍然存在于 EF 6.0 中:

https://msdn.microsoft.com/en-us/library/microsoft.data.entity.design.resources.mappingdetails_errcomplextypepropertiesnotsupported(v=vs.113).aspx#P:Microsoft.Data.Entity.Design.Resources.MappingDetails_ErrComplexTypePropertiesNotSupported

版本历史并未表明他们已经解决了这个问题:

https://msdn.microsoft.com/en-us/library/jj574253(v=vs.113).aspx

我发现了一个可以追溯到 2010 年的帖子 "it's in the backlog":

https://social.msdn.microsoft.com/Forums/en-US/c412a7d7-9455-47bc-87dd-46d3f0809b1b/mapping-a-stored-procedure-to-an-entity-that-contains-complex-types?forum=adodotnetentityframework

使用 VS2013 和最新的 EF 进行的快速本地测试证实了这一点: