EF4.1 多个嵌套实体 Includes 获取 NotSupportedException?
编辑:根据测试更新问题描述 - 2011 年 9 月 12 日。
每当我调用 .ToList() 时,我都会遇到此查询,该查询会抛出 NotSupportedException(“不支持指定的方法。”)。
IQueryable<FileDefinition> query = db
.FileDefinitions
.Include(x => x.DefinitionChangeLogs)
.Include(x => x.FieldDefinitions.Select(y => y.DefinitionChangeLogs)) // bad
.Include(x => x.FieldDefinitions.Select(y => y.FieldValidationTables)) // bad
.Where(x => x.IsActive);
List<FileDefinition> retval = query.ToList();
如果我注释掉我注释为“坏”的任何一行,那么查询就可以工作。我还尝试在我的对象模型中包含具有相同效果的不同嵌套实体。包含任何 2 个都会导致崩溃。我所说的嵌套是指导航属性的导航属性。我还尝试使用带有字符串路径的 .include 方法:结果相同。
我的表结构如下所示:
这是使用 MySQL 5.1(显然是 InnoDB 表)作为 MySQL Connector/NET 6.3.4 的数据库存储。
所以我的问题是:为什么这不起作用?
注意:如果我像 此链接。但我想知道为什么 EF 讨厌我的数据模型。
答案:MySQL 连接器显然无法处理第二个嵌套实体包含。它抛出 NotSupportedException,而不是 .NET EF。当我使用 EF4.0 尝试此操作时,也出现了同样的错误,但当时的研究使我相信是自我跟踪实体导致了该问题。我尝试升级到最新的连接器,但它开始导致不同步错误。这是我讨厌 MySQL 的另一个原因。
Edit: Updated problem description based on testing - 12 Sep 2011.
I have this query that throws a NotSupportedException ("Specified method is not supported.") whenever I call .ToList().
IQueryable<FileDefinition> query = db
.FileDefinitions
.Include(x => x.DefinitionChangeLogs)
.Include(x => x.FieldDefinitions.Select(y => y.DefinitionChangeLogs)) // bad
.Include(x => x.FieldDefinitions.Select(y => y.FieldValidationTables)) // bad
.Where(x => x.IsActive);
List<FileDefinition> retval = query.ToList();
If I comment out either line that I have commented as "bad", then the query works. I have also tried including different nested entities in my object model with the same effect. Including any 2 will cause a crash. By nested, I mean a navigation property of a navigation property. I also tried using the .Include methods with a string path: same result.
My table structure looks like this:
This is using MySQL 5.1 (InnoDB tables obviously) as the database store with MySQL Connector/NET 6.3.4.
So my question is: Why doesn't this work?
Note: I can get it to work if I explicitly load the related entities like in this link. But I want to know why EF hates my data model.
ANSWER: MySQL Connector is apparently not capable of handling the 2nd nested entity include. It throws the NotSupportedException, not .NET EF. This same error was also present when I tried this using EF4.0, but my research at the time led me to believe it was self-tracking entities causing the issue. I tried upgrading to latest Connector, but it started causing an Out of Sync error. This is yet another reason for me to hate MySQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许有点晚了,但我发现以下解决方法在当前项目中相当有用:
其中不使用第二行包含,而是使用 Select 返回原始导航属性,使用另一个 Select 前进到您的属性需要包括。
Maybe a little late to the party but i found the following workaround fairly useful in a current project:
Where rather than using a second row of includes, use Select to get back to the original navigation property and another Select to go forwards to the property you need to include.
我制作了一个小控制台应用程序来测试您的场景,并且此测试应用程序有效:
我的理解是,这基本上代表您的模型和您正在尝试的查询(还考虑您对问题的评论)。但某个地方一定存在重要的差异,该差异在您问题的代码片段中不可见,并且导致您的模型无法工作。
编辑
我已经使用 MS SQL 提供程序(SQL Server 2008 R2 Express DB)而不是 MySQL Connector 测试了上面的工作控制台应用程序。显然这是“重要的区别”。
I have made a little console application to test your scenario and this test application works:
My understanding was that this basically represents your model and the query you are trying (taking also your comments to your question into account). But somewhere must be an important difference which is not visible in the code snippets in your question and which makes your model fail to work.
Edit
I have tested the working console application above with MS SQL provider (SQL Server 2008 R2 Express DB), not MySQL Connector. Apparently this was the "important difference".