0

我正在使用.Net 4.0 EF 4.0。

我在数据库 myentity、mygroup 和 myapplication 中有三个表。myentity 可能属于 1 到更多 mygroups,并且有 1 到更多 myapplications 并映射到 EF。然后我有一个 myentityrepository 类公开一个方法,如下所示:

    public MyEntity GetByName(string name)
    {
        var v = Set().Where(x => x.Name == name)
            .Include(x => x.MyEntityMyApplications)
            .Include(x => x.MyEntityMyGroups);
        v.Load();
        //v.ToList();

        return v.FirstOrDefault();
    }

在我的 MVC controller.cs 中,我得到了名称并用 myentity 对象填充了我的视图模型。返回查看页面后,我得到了上面的错误。

     [HttpGet]
     public ActionResult GetMyentityAjax(string name)
     {
          MyViewModel uvm = new MyViewModel();
        using (IUnitOfWork unitOfWork = new UnitOfWork(new MyentityEntityContextFactory()))
        {
             Myentity me = _myentityRepository.GetByName(name);

            if (me != null)
            {
                uvm.Name = me.Nme;
                 ...                      
                 uvm.MyentityMyApplication = me.MyentityMyApplication.ToList();
                uvm.MyentityMygroups = me.MyentityMygroups .ToList();
            }
        }
        return Json(uvm, JsonRequestBehavior.AllowGet);         }
      //Even turned the lazyloding to false in //MydataModel.designer.cs as below
     public MyappEntities() : base("name=MyappEntities", "MyappEntities")
      {
        //#### CHANGE ME ######
        //this.ContextOptions.LazyLoadingEnabled = true;
        this.ContextOptions.LazyLoadingEnabled = false;
        //#### END #####
        OnContextCreated();
      }

请帮忙。TIA,
-t

4

1 回答 1

0

在 mydatamodel.designer.cs 中将所有三个 this.ContextOptions.LazyLoadingEnabled 更改为 false 后,异常消失了。

于 2013-05-08T01:14:13.700 回答