实体框架4.1 - TPT预先加载 - "指定表达式的与resultType是不符合要求的类型&QUOT兼容; [英] Entity Framework 4.1 - TPT Eager Loading - "The ResultType of the specified expression is not compatible with the required type"

查看:140
本文介绍了实体框架4.1 - TPT预先加载 - "指定表达式的与resultType是不符合要求的类型&QUOT兼容;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有TPT继承的典范。

I have a model with TPT inheritance.


  • 位置摘要的)

  • 街景来源于位置的)

  • GoogleStreetView 1 - > 0,1 GoogleStreetView 的)

  • Location (abstract)
  • Street (derived from Location)
  • GoogleStreetView (1 Street -> 0..1 GoogleStreetView)

每个上面都有它自己的表。

Each of the above has it's own table.

所有,直到我添加了GoogleStreetView表(由PK / FK到街道的支持)是工作的罚款。

All was working fine until i added the "GoogleStreetView" table (which is backed by a PK/FK to Street).

当我尝试这样做:

var street = _locationRepository
   .Find()
   .OfType<Street>()
   .Include(x => x.GoogleStreetView)
   .SingleOrDefault(x => x.LocationId == 1);



我得到的错误:

I get the error:

指定表达式的与resultType是不符合要求的类型兼容。表达与resultType是'Transient.reference [xxxx.Repositories.SqlServer.Location]',但所需要的类型是Transient.reference [xxxx.Repositories.SqlServer.Street]。
参数名:参数[0]

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]

什么...

后来我发现的这基本上指出,这是一个错误>与EF 4(和EF 4.1 RTM,通过它的外观)。

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).

我不明白的解决办法使用一个独立的协会,没有一个支持FK。

I don't understand the workaround of "use an independant association, without a backing FK".

我使用Repository / UOW模式,所以我LocationRepository只能访问对象集<地点> 。所以我不能做LINQ查询明确连接。

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?

推荐答案

好吧,我已经找到了一个解决办法。

Okay, i've found one workaround.

这是围绕翻转FK。

因此,而不是:

1 街景 - > 0..1 GoogleStreetView

1 Street -> 0..1 GoogleStreetView

这是正确的,我现在有:

Which is correct, i now have:

的*街 - > ** 0..1 GoogleStreetView

* Street -> **0..1 GoogleStreetView

所以现在街景有一个可为空FK指向GoogleStreetView。

So now Street has a nullable FK pointing to GoogleStreetView.

这工作,并从代码的角度行为罚款,但是从数据库角度来看,这是完全错误的,因为这种缺乏参照完整性,一个特定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.

这似乎只是EF不支持1 - 0..1协会通过PK / FK组合支持

It just seems like EF does not support 1 - 0..1 associations backed by a PK/FK combo.

不可接受的。如果他们知道这是一个错误的EF 4.0,为什么他们不解决它在4.1 ???

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???

修改

此外,上面的解决方法将工作,不愉快打破了概念上的侧参照完整性。

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.

这篇关于实体框架4.1 - TPT预先加载 - &QUOT;指定表达式的与resultType是不符合要求的类型&QUOT兼容;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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