2013-05-07 65 views
0

我正在使用.Net 4.0 EF 4.0。已處置ObjectContext實例

我在數據庫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

回答

0

將mydatamodel.designer.cs中的所有三個this.ContextOptions.LazyLoadingEnabled更改爲false後,異常消失。

相關問題