对多对多关系的查询失败不一致 [英] Query On Many-To-Many Relations Fails Inconsistently

查看:77
本文介绍了对多对多关系的查询失败不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


 


我创建了包含2个实体的实体框架模型(包含EF3),其中包含许多 - 多对多关系(例如产品< - >订单)。该关系由DB中的3个表覆盖。


我正在尝试使用所有订单查询Product(使用包含Product对象的方法)。


 


结果是不存在的:在大多数情况下,查询会生成所有相关对象(产品和订单),但在某些情况下,产品并没有退回订单(对于同一产品) )。


 


任何想法如何解决这样的谜团? :):):)


跟踪问题的有效方法是什么?


 


 


谢谢,


标记

解决方案

Hello Mark,


 


Welcome到ADO.NET实体框架和LINQ to Entities论坛!


 


我认为问题非常奇怪。  
我认为EF3意味着EF的第一个版本,你使用的是VS2008 SP1,对吗?
  
J    
我建议你通过
检查EF查询生成的SQL语句
ObjectQuery<> .ToTraceString()


=========================================== ========================================


var query =
来自 p context.Products.Include(" Orders"


    & NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
选择 p;


< span style ="font-family:新宋体;颜色:#2b91af; font-size:9pt">控制台 。WriteLine( (( ObjectQuery < Product >)query).ToTraceString());
===================== ================================================== ============


有关其他信息,请参阅
http://msdn.microsoft.com/en-us/library/bb896348.aspx   


 


另外,我们可以使用
SQL Server Profiler 用于跟踪SQL语句。&NBSP;
通常,SQL语句可以是三个表的LEFT OUTER JOIN,因为它是Product和Order表之间的多对多关系。 


 


 


BTW,在EF4(VS2010)中,我们为相关实体提供了延迟加载功能。  
因此,如果我们尝试访问它们,我们就不需要调用.Include()和相关实体。 
详情请查看


http://thedatafarm.com/blog/data-access/a-look-at-lazy-loading-in-ef4/


http://geekswithblogs.net/iupdateable/archive/2009/11/26/getting-started- with-entity-framework-4-ndash-lazy-loading.aspx


 


 


如果您有任何疑问,请随时告诉我。


 


祝你有愉快的一天!


 


 


最诚挚的问候,b
Lingzhi Sun


MSDN用户支持
在论坛


< span style ="font-size:12pt"> 如果您对我们的支持有任何反馈,请联系
msdnmg@microsoft.com


Hi,

 

I created Entity Framework model (with EF3) with 2 entities governed by many-to-many relation (e.g. Products <-> Orders). The relation is covered by 3 tables in DB.

I'm trying to query Product with all Orders (using Include method with Product object).

 

The result is incosistent: in most cases the query produces all the related objects (Product and Orders), however in some cases a product and no orders returned (for the same product).

 

Any ideas how to resolve such a mystery? :):):)

What is a efficient way to trace the problem?

 

 

Thank you,

Mark

解决方案

Hello Mark,

 

Welcome to ADO.NET Entity Framework and LINQ to Entities forum!

 

I think the problem is really strange.   I think EF3 means the first version of EF and you are using VS2008 SP1, right?   J    I would recommend you check the SQL statements generated by the EF query via ObjectQuery<>.ToTraceString():

===================================================================================

var query = from p in context.Products.Include("Orders")

            select p;

Console.WriteLine(((ObjectQuery<Product>)query).ToTraceString()); ===================================================================================

For additional information, please see http://msdn.microsoft.com/en-us/library/bb896348.aspx.  

 

Also, we can use SQL Server Profiler to trace the SQL statements.  Generally, the SQL statements can be LEFT OUTER JOIN cross three tables since it is the many-to-many relationship between the Product and Order tables. 

 

 

BTW, in EF4 (VS2010) we have the lazy loading feature for the related entities.   So we don’t need to call .Include() and the related entities will be loaded if we try to access them.  For detail, please check

http://thedatafarm.com/blog/data-access/a-look-at-lazy-loading-in-ef4/

http://geekswithblogs.net/iupdateable/archive/2009/11/26/getting-started-with-entity-framework-4-ndash-lazy-loading.aspx

 

 

If you have any questions, please feel free to let me know.

 

Have a nice day!

 

 

Best Regards,
Lingzhi Sun

MSDN Subscriber Support in Forum

If you have any feedback on our support, please contact msdnmg@microsoft.com


这篇关于对多对多关系的查询失败不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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