实体框架 4 —— ExecuteFunction的问题FunctionImport 映射到 POCO 复杂类型
我在尝试从自定义 ObjectContext
中执行 FunctionImport
时收到错误 - 结果映射到 POCO ComplexType。具体来说,错误指出:
ExecuteFunction 中的类型参数
BlahComplexType
与函数返回的类型BlahComplexType
不兼容。
因为我使用的是自定义 ObjectContext
(我正在使用完全 POCO 实体框架环境),所以我必须手动调用 FunctionImport
,我确实喜欢
var blah = ExecuteFunction<BlahComplexType>("GetBlah", MergeOption.NoTracking,
new ObjectParameter("p_one", paramOne),
new ObjectParameter("p_two", paramTwo),
new ObjectParameter("p_three", string.Empty));
:不使用任何T4模板来生成POCO,所有类都是手动编写的。 BlahComplexType
是一个具有简单数据类型属性的类。它的定义与 .edmx 文件中的 ComplexType 定义匹配,包括匹配的命名空间和所有内容(我还有很多其他实体和复杂类型映射到 POCO)。
msdn文档此处指出ExecuteFunction<的返回类型为T>
必须实现 IEntityWithChangeTracker
,但这并不是映射到 ComplexType 而不是 Entity 的全部意义,正是因为我不关心任何更改(事实上,更改是不允许——BlahComplexType 被视为“值对象”)?
是否有人有幸使用 ExecuteFunction 调用 FunctionImport,其结果使用 Entity Framework 4 映射到 POCO ComplexType?
I am receiving an error when trying to execute a FunctionImport
-- result is mapped to a POCO ComplexType -- from within a custom ObjectContext
. Specifically, the error states:
The type parameter
BlahComplexType
in ExecuteFunction is incompatible with the typeBlahComplexType
returned by the function.
Because I'm using a custom ObjectContext
(I'm working with a fully POCO Entity Framework environment), I must call the FunctionImport
manually, which I do like:
var blah = ExecuteFunction<BlahComplexType>("GetBlah", MergeOption.NoTracking,
new ObjectParameter("p_one", paramOne),
new ObjectParameter("p_two", paramTwo),
new ObjectParameter("p_three", string.Empty));
I am not using any T4 templates to generate POCOs, all classes are manually written. BlahComplexType
is a class with simple data type properties. Its definition matches the ComplexType definition in the .edmx file, including matching namespaces and everything (I've got plenty of other Entities and Complex Types mapped to POCOs, as well).
The msdn documentation states here that the return type of ExecuteFunction<T>
must implement IEntityWithChangeTracker
, but isn't the whole point of mapping to a ComplexType instead of an Entity is precisely because I don't care about any changes (in fact, changes are not allowed -- BlahComplexType is considered a "Value Object")?
Has anyone had any luck calling a FunctionImport with ExecuteFunction whose results are mapped to a POCO ComplexType with Entity Framework 4?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇,愚蠢的用户错误!!
与手动创建 POCO 类一样,EDMX 文件中的 ComplexType 定义与其 POCO 类之间存在不一致。如果只有一个错误或不一致,则错误消息会尝试告诉您问题的根源。但是,如果您有多个类型,它只会声明这些类型“不兼容”,因此您会感到疑惑。
就我而言,我同时遇到属性名称不一致(一个具有“AcctId”,另一个具有“Acctid”)和属性数据不一致(类期望 int (Int32) 但存储过程返回 Int16)。
因此,一如既往,仔细检查 EDMX 定义和 POCO 类定义。 (我仔细检查过,但忽略了三重检查!!)
捂脸
Wow, stupid user error!!
As always with manual creation of the POCO classes -- it was an inconsistency between the ComplexType definition in the EDMX file and its POCO class. If there's just one error or inconsistency, then the error message makes an attempt at telling you the source of the problem. However, if you have more than one, it'll just state that the types are "incompatible", so you're left wondering.
In my case, I had both a property name inconsistency (one had "AcctId", the other had "Acctid") and a property data inconsistency (class was expecting int (Int32) but the stored procedure returned Int16).
So, as always, double and triple check EDMX definition and POCO class definition. (I double checked, but neglected to triple check!!)
facepalm