开发者

Linq query returning Less records than Sql Query

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 开发者_如何学Cif 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..


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

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.


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 of LEFT OUTER joins.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜