LazyLoadingEnabled设置在EF 5中似乎不起作用 [英] LazyLoadingEnabled setting doesn't seem to work in EF 5

查看:204
本文介绍了LazyLoadingEnabled设置在EF 5中似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF模型首先使用POCO实体和自定义DbContexts。我的问题是设置 LazyLoadingEnabled = false 不影响任何东西,导航属性仍然​​加载。
下面是我的例子简化。



实体计划。程序可以是其他程序的一部分:

 命名空间Domain.Entities 
{
using System;
使用System.Collections.Generic;

public partial class Program
{
public Program()
{
this.Programs = new HashSet< Program>();
}

public int Id {get;组; }
public string标题{get;组; }
public string描述{get;组; }
public System.DateTime StartDate {get;组; }
public System.DateTime EndDate {get;组; }
public Nullable< int> ProgramId {get;组; }

public virtual ICollection< Program>节目{get;组; }
public virtual Program OwnerProgram {get;组;
}
}

DbContext:

 命名空间Infrastructure.Model 
{
public class ProgramContext:DbContext
{
public ProgramContext()
:base(name = MyContainer)
{
Configuration.LazyLoadingEnabled = false;
}

public DbSet< Program>节目{get;组; }
}
}

这是我如何使用它:

  private ProgramContext _dbContext = new ProgramContext(); 

// GET api / program
public IEnumerable< Program> GetPrograms()
{
列表<程序> list = _dbContext.Programs.ToList();
返回列表;
}

使用上面的示例,EF仍然加载程序的程序和OwnerProgram属性类。我已经尝试删除虚拟关键字,禁用代理创建,并验证了模型本身的 LazyLoadingEnabled = false



我是否缺少某些东西?

解决方案

您看到的效果称为关系fixup / p>

实际上导航属性不是明确加载。查询 _dbContext.Programs.ToList()仅从数据库加载整个程序表。这只是一个简单的SQL查询(如 SELECT * FROM ProgramsTable ),没有任何 WHERE 子句,没有任何 JOIN 到相关行。



此外,还没有懒惰加载(如果禁用它,它真的不能,如果禁用甚至是动态代理)当您访问 program.Programs program.OwnerProgram 导航属性。



当您的查询的结果实现时,导航属性将被填充,因为您的查询(加载所有程序)将加载导航属性可以引用的所有程序。 EF检测到这些相关实体已经在内存中,并自动将它们导入导航属性。



如果您不加载全部程序,但只有一个:

 程序程序= _dbContext.Programs.FirstOrDefault(); 

现在, program.Programs program.OwnerProgram 将是 null - 除非加载的程序自己的 program.OwnerProgram collection或是它自己的 OwnerProgram


I'm using EF Model first with POCO entities and with custom DbContexts. My problem is that setting LazyLoadingEnabled=false does not affect anything, navigation properties are still loaded. Below is my example simplified.

The entity Program. A program can be part of other programs:

namespace Domain.Entities
{
    using System;
    using System.Collections.Generic;

    public partial class Program
    {
        public Program()
        {
            this.Programs = new HashSet<Program>();
        }

        public int Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public System.DateTime StartDate { get; set; }
        public System.DateTime EndDate { get; set; }
        public Nullable<int> ProgramId { get; set; }

        public virtual ICollection<Program> Programs { get; set; }
        public virtual Program OwnerProgram { get; set; }
    }
}

The DbContext:

namespace Infrastructure.Model
{
    public class ProgramContext : DbContext
    {
        public ProgramContext()
            : base("name=MyContainer")
        {
            Configuration.LazyLoadingEnabled = false;
        }

        public DbSet<Program> Programs { get; set; }
    }
}

Here is how I use it:

private ProgramContext _dbContext = new ProgramContext();

// GET api/program
public IEnumerable<Program> GetPrograms()
{
    List<Program> list = _dbContext.Programs.ToList();
    return list;
}

With the above sample, EF still loads the Programs and OwnerProgram properties of the Program class. I have tried removing the virtual keywords, disabling the proxy creation and also verified that LazyLoadingEnabled=false on the Model itself.

Am I missing something?

解决方案

The effect you are seeing is called relationship fixup.

Actually the navigation properties are not loaded explicitly. The query _dbContext.Programs.ToList() only loads the whole Programs table from the database. This is just a simple SQL query (like SELECT * FROM ProgramsTable) without any WHERE clause and without any JOIN to related rows.

Also no lazy loading happens here (it really can't if you disable it and if you disable even dynamic proxies) when you access the program.Programs and program.OwnerProgram navigation properties.

The navigation properties get populated when the result from your query is materialized because your query (that loads all programs) will have loaded all programs that the navigation properties can refer to. EF detects that those related entities are already in memory and put them into the navigation properties automatically.

You can verify this if you don't load all programs but only, for example, a single one:

Program program  = _dbContext.Programs.FirstOrDefault();

Now, program.Programs and program.OwnerProgram will be null - unless the loaded program is part of its own program.OwnerProgram collection or is its own OwnerProgram.

这篇关于LazyLoadingEnabled设置在EF 5中似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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