EF4.1多个嵌套实体包含获取NotSupportedException? [英] EF4.1 multiple nested entity Includes gets NotSupportedException?

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

问题描述

编辑:基于测试的更新的问题描述 - 2011年9月12日。

Updated problem description based on testing - 12 Sep 2011.

我有这个查询引发了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连接器显然不能处理第二个嵌套实体。它抛出NotSupportedException,而不是.NET EF。当我尝试使用EF4.0时,也出现了同样的错误,但是当时我的研究使我相信这是自我跟踪实体造成的问题。我尝试升级到最新的连接器,但它开始导致不同步错误 。这是另一个原因,我讨厌MySQL。 / strong>

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 Express DB)测试了上述工作控制台应用程序,而不是MySQL连接器。显然这是重要差异。

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天全站免登陆