实体框架 4.1 - TPT 预加载 - “指定表达式的 ResultType 与所需类型不兼容”
我有一个具有 TPT 继承的模型。
- 位置(抽象)
- 街道(派生自位置)
- GoogleStreetView(
1
Street ->0..1
GoogleStreetView)
以上每个都有自己的桌子。
一切都工作正常,直到我添加了“GoogleStreetView”表(该表由 PK/FK 到 Street 支持)。
当我尝试这样做时:
var street = _locationRepository
.Find()
.OfType<Street>()
.Include(x => x.GoogleStreetView)
.SingleOrDefault(x => x.LocationId == 1);
我收到错误:
指定表达式的 ResultType 与所需类型不兼容。表达式 ResultType 为“Transient.reference[xxxx.Repositories.SqlServer.Location]”,但所需类型为“Transient.reference[xxxx.Repositories.SqlServer.Street]”。 参数名称:arguments[0]
什么...?
然后我发现 此线程 基本上表明这是 EF 4(以及 EF 4.1 RTM,从外观上看)的一个错误。
我不明白“使用独立协会,没有支持 FK”的解决方法。
我使用 Repository / UoW 模式,因此我的 LocationRepository 只能访问 ObjectSet
。所以我无法在 LINQ 查询中进行显式联接。
在这个阶段,看起来我根本不必映射该表,而是使用存储过程从数据库中获取它。叹。
任何人都可以阐明这一点并提供解决方案吗?
I have a model with TPT inheritance.
- Location (abstract)
- Street (derived from Location)
- GoogleStreetView (
1
Street ->0..1
GoogleStreetView)
Each of the above has it's own table.
All was working fine until i added the "GoogleStreetView" table (which is backed by a PK/FK to Street).
When i try and do this:
var street = _locationRepository
.Find()
.OfType<Street>()
.Include(x => x.GoogleStreetView)
.SingleOrDefault(x => x.LocationId == 1);
I get the error:
The ResultType of the specified expression is not compatible with the required type. The expression ResultType is 'Transient.reference[xxxx.Repositories.SqlServer.Location]' but the required type is 'Transient.reference[xxxx.Repositories.SqlServer.Street]'.
Parameter name: arguments[0]
What the...?
Then i found this thread which basically states this is a bug with EF 4 (and EF 4.1 RTM, by the looks of it).
I don't understand the workaround of "use an independant association, without a backing FK".
I use the Repository / UoW pattern, so my LocationRepository only has access to ObjectSet<Location>
. So i can't do explicit joins in LINQ queries.
At this stage, it's looking like i'll have to not map this table at all, and go fetch it from the database using a stored procedure. Sigh.
Can anyone shed some light on this, and offer a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到了一种解决方法。
那就是翻转FK。
所以而不是:
这是正确的,我现在有:
所以现在 Street 有一个指向 GoogleStreetView 的可为 null 的 FK。
从代码的角度来看,这种方法工作得很好,但从数据库的角度来看,这是完全错误的,因为缺乏引用完整性,一个特定的 GoogleStreetView 记录可能指向多个街道记录,这没有任何意义。
但这看起来是唯一正确的解决方法。
EF 似乎不支持 PK/FK 组合支持的 1 - 0..1 关联。
如果你问我的话,我无法接受。如果他们知道这是 EF 4.0 中的错误,为什么不在 4.1 中修复它???
编辑
上述解决方法也将起作用,但对破坏概念方面的引用完整性不满意。
因此,我决定根本不映射该实体,而是通过存储过程从数据库中获取它。
Okay, i've found one workaround.
And that is to flip the FK around.
So instead of:
Which is correct, i now have:
So now Street has a nullable FK pointing to GoogleStreetView.
And that works and acts fine from a code perspective, but from a database perspective this is totally wrong, as with this lack of referential integrity, one particular GoogleStreetView record could point to multiple Street records, which doesn't make any sense.
But this looks like the only proper workaround.
It just seems like EF does not support 1 - 0..1 associations backed by a PK/FK combo.
Unacceptable if you ask me. If they knew this was a bug in EF 4.0, why didn't they fix it in 4.1???
EDIT
Also the above workaround will work, not happy with breaking the referential integrity in the conceptual side.
So i've decided to not map this entity at all and go fetch it from the DB via a stored procedure.