简体   繁体   English

在EF 4.1 eager loading中,如何加载多个(同级别)孙子关系?

[英]In EF 4.1 eager loading, how can I load multiple (same-level) grandchild relationships?

I'm seemingly stumped by a likely simple solution. 我似乎被一个可能简单的解决方案所困扰。 I've typically used lazy loading in EF 4.1, and I'm now trying to use eager loading in my application so I can use the built-in JSON serializer without issues. 我通常在EF 4.1中使用延迟加载,现在我正在尝试在我的应用程序中使用预先加载,因此我可以使用内置的JSON序列化器而不会出现问题。 The problem I am having is that I can't figure out how to use .Include() to load multiple same-level grandchild relationships. 我遇到的问题是我无法弄清楚如何使用.Include()来加载多个同级孙子关系。

public class Canvas
{
    public int ID { get; set; }
    public string Name { get; set; }

    public ICollection<ContentArea> Contents { get; set; }
}

public class ContentArea
{
    public int ID { get; set; }
    public int CanvasID { get; set; }
    public string Name { get; set; }

    public ICollection<TextContent> TextContents { get; set; }
    public ICollection<ImageContent> ImageContents { get; set; }
}

public class TextContent
{
    public int ID { get; set; }
    public int ContentAreaID { get; set; }
    public string Text { get; set; }
    public int Color { get; set; }
}

public class ImageContent
{
    public int ID { get; set; }
    public int ContentAreaID { get; set; }
    public string Filename { get; set; }
}

I've tried the following with no success. 我试过以下但没有成功。 How can I write the loading code to load TextContents and ImageContents? 如何编写加载代码来加载TextContents和ImageContents?

Doesn't compile: 不编译:

var c = dataContext.Canvases
        .Include(ca => ca.Contents.Select(co => co.ImageContents).Select(co => co.TextContents))
        .FirstOrDefault();

Doesn't work, second Include overrides first: 不起作用,第二个包含覆盖优先:

var c = dataContext.Canvases
        .Include(ca => ca.Contents.Select(co => co.ImageContents))
        .Include(ca => ca.Contents.Select(co => co.TextContents))
        .FirstOrDefault();

Doesn't work, throws runtime exception: 不起作用,抛出运行时异常:

var c = dataContext.Canvases
        .Include(ca => ca.Contents.Select(co => new { co.ImageContents, co.TextContents }))
        .FirstOrDefault();

Edit: 编辑:

I've given up on this approach for now and just made view models based on some other articles and approaches they have taken solving the "serializing Entity models" problem with the ASP.NET MVC built in JSON serialization. 我现在已经放弃了这种方法,只是基于其他一些文章和方法制作了视图模型,这些文章和方法解决了使用JSON序列化构建的ASP.NET MVC的“序列化实体模型”问题。 This caused me to duplicate my classes, but it was made easy by using the AutoMapper library to transfer all of the data back and forth automatically. 这导致我复制我的类,但通过使用AutoMapper库自动来回传输所有数据,这很容易。

I have succeeded with following codes 我已成功完成以下代码

var contents = db.Canvases
                 .Include(c=>c.Contents.Select(co=>co.TextContents))
                 .Include(c=>c.Contents.Select(co=>co.ImageContents))
                 .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM