【问题标题】:Lazy loading does not work in entity framework 5延迟加载在实体框架 5 中不起作用
【发布时间】:2013-01-01 21:30:15
【问题描述】:

我在我的域项目中定义了一个(poco?)类:

public class Club
{
   public Club()
   {
      ContactPersons = new HashSet<ContactPerson>();
   }

   public int Id { get; set; }

   [Required]
   [StringLength(64)]
   public string Name { get; set; }

   public virtual ICollection<ContactPerson> ContactPersons { get; set; }
}

public class ContactPerson
{
     public virtual int Id { get; set; }

     [StringLength(64)]
     public virtual string FirstName { get; set; }

     [StringLength(64)]
     public virtual string LastName { get; set; }
 }

在我的 MVC 项目中,我有我的俱乐部控制器:

   public ActionResult Create(CreateClubViewModel model)
   {
      Club club = new Club();
      model.Initialize(club);
      IClubDb clubDb = DependencyResolverHelper.IClubDbService;
      clubDb.Create(club);  // create club in db
   }

    public ActionResult Display(string domain)
    {
        try
        {
            IClubDb clubDb = DependencyResolverHelper.IClubDbService;
            Club club = clubDb.Get(domain);
            return View(club);
        }
        catch (Exception)  // user is not logged iin
        {
            return View();
        }
    }

最后,在我的数据库项目中,我创建并检索了俱乐部,

public Club Get(string name)
{
   return DataContext.Clubs
   //.Include(x => x.ContactPersons)
   .Single(r => r.Name == name);
}

 public int Create(Club club)
 {
         DataContext.Clubs.Add(club);
         return DataContext.SaveChanges();
 }

当我在 Display 方法中调用 Get club 时,我已尽一切努力让 EF 延迟加载我的俱乐部对象的 ContactPersons,但 ContactPersons 的长度始终为零。但是,如果我渴望使用 .include 加载联系人(我已将这部分注释掉),那么显然 ContactPersons 包含许多联系人。

我不确定我做错了什么:

  1. 我遵循了定义 poco 类的指南:http://msdn.microsoft.com/en-us/library/dd468057.aspx
  2. 我有一个公共参数少的构造函数(但不是受保护的构造函数)
  3. 我启用了延迟加载

我想我错过了一个概念,poco club 类也是我插入数据库的域实体。我究竟做错了什么?为什么我不能让延迟加载工作?

【问题讨论】:

  • 尝试使用List&lt;ContactPerson&gt; 而不是ICollection&lt;ContactPerson&gt;。我认为 EF 不够聪明,无法使用 ICollection 的
  • Dominic,刚刚试过你的建议,还是一样,ContactPersons 为空:(
  • @Dominic: ICollection&lt;T&gt; 工作得很好。它甚至更有意义,因为 List 意味着一个顺序,并且相关实体不会以任何特定顺序返回。
  • 您是否启用了代理创建?
  • 是的 Jayantha,当我初始化数据库上下文时,我有这两行:this.Configuration.LazyLoadingEnabled = true; this.Configuration.ProxyCreationEnabled = true;

标签: asp.net-mvc entity-framework lazy-loading


【解决方案1】:

试试 ContactPersons.ToList();

这将强制加载所有实体。 见Entity framework, when call ToList() it will load FK objects automatically?

【讨论】:

    【解决方案2】:

    似乎您的 LazyLoading 在您的 dbContext 关闭时执行。所以它不会加载。 您在视图中使用 ContactPerson,对吗?

    【讨论】:

    • 是的,我在 Display 方法的 View(club) 中访问俱乐部。但是我确实尝试在“返回视图(俱乐部”)行之前访问联系人,并且是一样的。当我在 DB 项目中获得俱乐部时,我也尝试访问联系人,但未加载。
    • 当我使用调试器时,ContactPerson 的类型没有改变。我依稀记得 EF 创建了一个新类型并在 getter 访问器函数中插入了一个钩子。似乎 EF 根本没有创建代理对象,因此延迟加载失败。我的代码中有以下几行: this.Configuration.LazyLoadingEnabled = true; this.Configuration.ProxyCreationEnabled = true;
    【解决方案3】:

    您是否忘记在您的实体中包含外键?

    public class ContactPerson
    {
        public virtual int Id { get; set; }
    
        [StringLength(64)]
        public virtual string FirstName { get; set; }
    
        [StringLength(64)]
        public virtual string LastName { get; set; }
    
        public int ClubId { get; set; }
        [ForeignKey("ClubId")]
        public virtual Club Club { get; set; } // if you need
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多