EF Code First Lazy加载不工作 [英] EF Code First Lazy loading Not Working

查看:110
本文介绍了EF Code First Lazy加载不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF6代码,但似乎无法懒惰加载工作。渴望加载工作正常。我有以下课程:

  public class商家:用户
{
...

public virtual ICollection< MerchantLocation> MerchantLocations {get;组;
}

public class MerchantLocation:BaseEntity
{
...

public int MerchantId {get;组; }
public virtual Merchant {get;组; }
}

public class User:BaseEntity
{
...
}

public class BaseEntity
{
...

public int Id {get;组; }
}

我通过以下代码测试我的懒惰加载的位置(失败):$ {

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b var merchant = context.Users.OfType< Merchant> .First();
merchant.MerchantLocations.ShouldNotBeNull(); //失败
}
}

但是,希望加载工作正常: p $

  public void Test_Eager_Loading(){
using(var context = new MyDbContext()){
var merchant = context.Users.OfType<商家GT; .INCLUDE( MerchantLocations)第一();
merchant.MerchantLocations.ShouldNotBeNull(); //通过
}
}

MerchantLocations 被标记为 public virtual ,所以我不知道是什么问题。我还在我的 DbContext 构造函数中添加了以下内容:

 配置。 LazyLoadingEnabled = true; 
Configuration.ProxyCreationEnabled = true;

编辑:我也注意到商家在上述测试中返回的对象不是EF代理。这是一个简单的商家。我怀疑这是导致这个问题。

解决方案

我意识到问题是商家类不符合代理要求代。具体来说,我需要添加一个受保护的无参数构造函数。我只有一个私人的。


I am using code first with EF6 but cannot seem to get lazy loading to work. Eager loading is working fine. I have the following classes:

public class Merchant : User
{
    ...

    public virtual ICollection<MerchantLocation> MerchantLocations { get; set; }
}

public class MerchantLocation : BaseEntity
{
    ...

    public int MerchantId { get; set; }
    public virtual Merchant Merchant { get; set; }       
}

public class User : BaseEntity
{
    ...
}

public class BaseEntity
{
    ...

    public int Id { get; set; }
}

I test my lazy loading of the locations via the following code (which fails):

public void Test_Lazy_Loading() {
    using (var context = new MyDbContext()) {
        var merchant = context.Users.OfType<Merchant>.First();
        merchant.MerchantLocations.ShouldNotBeNull(); // fails
    }
}

However eager loading works fine:

public void Test_Eager_Loading() {
    using (var context = new MyDbContext()) {
        var merchant = context.Users.OfType<Merchant>.Include("MerchantLocations").First();
        merchant.MerchantLocations.ShouldNotBeNull(); // passes
    }
}

MerchantLocations is marked as public virtual so I'm not sure what the problem is. I have also added the following in my DbContext constructor:

Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;

edit: I have also noticed that the merchant object being returned in the above tests is not an EF proxy. It is a plain Merchant. I suspect that this is causing the problem.

解决方案

I realized that the problem was that the Merchant class did not meet requirements for proxy generation. Specifically, I needed to add a protected parameterless constructor. I only had a private one.

这篇关于EF Code First Lazy加载不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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