EF4.1多个嵌套实体包括得到NotSupportedException异常? [英] EF4.1 multiple nested entity Includes gets NotSupportedException?

查看:243
本文介绍了EF4.1多个嵌套实体包括得到NotSupportedException异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:根据测试更新问题说明 - 2011年9月12日

我有此查询引发NotSupportedException(指定的方法。 。不支持),每当我打电话.ToList()

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();

如果我注释掉要么线,我评价为差,那么查询工作。我也试过,包括我具有相同效果的对象模型不同的嵌套实体。包括任何2会导致崩溃。通过嵌套的,我的意思是导航属性的导航属性。我也尝试使用带有字符串路径.INCLUDE方法:同样的结果。

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.

我的表结构是这样的:

这是使用MySQL 5.1(InnoDB表明显) 。与的MySQL Connector / NET 6.3.4数据库存储

This is using MySQL 5.1 (InnoDB tables obviously) as the database store with MySQL Connector/NET 6.3.4.

所以我的问题是:为什么不这部作品

注:我可以得到它,如果我明确地加载相关实体像的此链接。但是,我想知道为什么EF恨我的数据模型

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.

答案:的MySQL Connector显然是无法处理的第二个嵌套的实体包括。它抛出NotSupportedException异常,没有.NET EF。同样的错误也在场,当我试图用这个EF4.0的,但我的研究在当时使我相信这是自跟踪实体导致问题。我想升级到最新的连接器,但它开始引起不同步误差的。这是的另一个原因我恨MySQL的。

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.

推荐答案

我已经做了小控制台应用程序来测试您的方案,并测试应用程序如下:

I have made a little console application to test your scenario and this test application works:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace EFIncludeTest
{
    public class Parent
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<ChildLevel1> ChildLevel1s { get; set; }
    }

    public class ChildLevel1
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<ChildLevel2a> ChildLevel2as { get; set; }
        public ICollection<ChildLevel2b> ChildLevel2bs { get; set; }
    }

    public class ChildLevel2a
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class ChildLevel2b
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<Parent> Parents { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Create entities to test
            using (var ctx = new MyContext())
            {
                var parent = new Parent
                {
                    Name = "Parent",
                    ChildLevel1s = new List<ChildLevel1>
                    {
                        new ChildLevel1
                        {
                            Name = "FirstChildLevel1",
                            ChildLevel2as = new List<ChildLevel2a>
                            {
                                new ChildLevel2a { Name = "FirstChildLevel2a" },
                                new ChildLevel2a { Name = "SecondChildLevel2a" }
                            },
                            ChildLevel2bs = new List<ChildLevel2b>
                            {
                                new ChildLevel2b { Name = "FirstChildLevel2b" },
                                new ChildLevel2b { Name = "SecondChildLevel2b" }
                            }
                        },

                        new ChildLevel1
                        {
                            Name = "SecondChildLevel1",
                            ChildLevel2as = new List<ChildLevel2a>
                            {
                                new ChildLevel2a { Name = "ThirdChildLevel2a" },
                                new ChildLevel2a { Name = "ForthChildLevel2a" }
                            },
                            ChildLevel2bs = new List<ChildLevel2b>
                            {
                                new ChildLevel2b { Name = "ThirdChildLevel2b" },
                                new ChildLevel2b { Name = "ForthChildLevel2b" }
                            }
                        },
                    }
                };

                ctx.Parents.Add(parent);
                ctx.SaveChanges();
            }

            // Retrieve in new context
            using (var ctx = new MyContext())
            {
                var parents = ctx.Parents
                    .Include(p => p.ChildLevel1s.Select(c => c.ChildLevel2as))
                    .Include(p => p.ChildLevel1s.Select(c => c.ChildLevel2bs))
                    .Where(p => p.Name == "Parent")
                    .ToList();

                // No exception occurs
                // Check in debugger: all children are loaded

                Console.ReadLine();
            }
        }
    }
}



我的理解是,这基本上代表了你的模型和你正试图查询(也把你的意见,你的问题考虑在内)。但地方必须是一个重要的区别是不可见的,在你的问题中的代码片断和使您的模型无法工作。

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.

修改

我已经测试以上MS SQL提供商(SQL Server 2008 R2的快递DB),没有的MySQL Connector工作控制台应用程序。显然,这是重要的区别。

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

这篇关于EF4.1多个嵌套实体包括得到NotSupportedException异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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