Linq 查询返回的记录少于 Sql 查询
我面临着简单 linq 查询的大问题..我正在使用 EF 4.0.. 我正在尝试使用 linq 查询从表中获取所有记录:
var result = context.tablename.select(x=>x);
这会导致比正常的 sql 查询更少的行,即 select * from tablename
;
该表有超过 5 个表作为子对象(外键关系:一对一和一对多等)。
执行该 linq 语句后,该结果变量返回包含所有子对象值的记录,而无需执行 include 语句。
我不不知道这是 EF 4.0 的默认行为..
我也在 linqpad 中尝试了这个语句..但是没有用...
但有趣的是,如果我在同一个表上与另一个表进行连接,则工作相同sql内连接和计数是相同的..但我不知道为什么它仅与该表的行为不同..
它是否在返回该父表的所有记录之前与所有子表进行内连接?
请帮我..
I am facing a big problem with simple linq query.. I am using EF 4.0..
I am trying to take all the records from a table using a linq query:
var result = context.tablename.select(x=>x);
This results in less rows than the normal sql query which is select * from tablename
;
This table has more than 5 tables as child objects (foreign key relations: one to one and one to many etc)..
This result variable after executing that linq statement returns records with all child object values without doing a include statement..
I don't know is it a default behavior of EF 4.0 ..
I tried this statement in linqpad also..but there is no use...
But interesting thing is if I do a join on the same table with another one table is working same is sql inner join and count is same..but I don't know why is it acting differently with that table only..
Is it doing inner joins with all child tables before returning the all records of that parent table??
please help me..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以我们可能正在谈论 SSDL 中的数据库视图或自定义
DefiningQuery
。我描述了相同这里的行为。基于连接表的实体可能没有每个重新调整的行的唯一标识,因此您的问题是身份映射。您必须手动配置实体的实体密钥。它应该是基于连接表中所有主键的复合键。实体键用于标识缩进图中的实体。如果每个记录没有唯一的密钥,则仅使用具有新密钥的第一条记录。如果您没有手动指定密钥,EF 会推断出它自己的密钥。
So we are probably talking about database view or custom
DefiningQuery
in SSDL.I described the same behavior here. Your entity based on joined tables probably doesn't have unique identification for each retruned row so your problem is Identity map. You must manually configure entity key of your entity. It should be composite key based on all primary keys from joined tables. Entity key is used to identify entity in indenty map. If you don't have unique key for each record only first record with the new key is used. If you didn't specify the key manually EF had infered its own.
解决此类问题的最简单方法是查看 ORM 工具生成的 SQL。
如果您使用的是 SQL Server,则使用 SQL Profiler 查看生成的 SQL。
根据您的描述,可能的解释可能是实体之间的关系是强制性的,从而强制执行
INNER
连接而不是LEFT OUTER
连接。The easiest way to troubleshoot these types of issues is to look at the generated SQL produced by the ORM tool.
If you are using SQL Server then using the
SQL Profiler
to view the generated SQL.From what you are describing, a possible explanation might be that your relationships between entities are mandatory and thereby enforcing
INNER
joins instead ofLEFT OUTER
joins.