2015-09-28 24 views
1

運行我的get方法在我的控制器列表時,我有以下錯誤:查找數據

指定的LINQ表達式包含引用是查詢與不同的上下文相關聯。

已經調試控制器它是產生錯誤的排序依據語句

方法是:

public ActionResult OwnerList() 
    {        
     var owners = (from s in db.Owners      
        orderby Peopledb.Posts.FirstOrDefault(x => x.PostId == s.PostId).PostName 
        select s).ToList(); 

     var viewModel = owners.Select(t => new OwnerListViewModel 
     { 
      Created = t.Created, 
      PostName = Peopledb.Posts.FirstOrDefault(x => x.PostId == t.PostId).PostName, 
      Dormant = t.Dormant, 
      OwnerId = t.OwnerId, 
     }); 
     return PartialView("_OwnerList", viewModel); 
    } 

在第一數據庫owner的類是,的DbContext = IARContext:

public class Owner 
{ 
    public int OwnerId { get; set; } 

    [Column(TypeName = "int")] 
    public int PostId { get; set; } 

    [Column(TypeName = "bit")] 
    public bool Dormant { get; set; } 

    [Column(TypeName = "datetime2")] 
    public DateTime Created { get; set; } 

    public virtual ICollection<Asset> Assets { get; set; } 

    public People.Models.Post Post { get; set; } 
} 

第二個數據庫中的Post類是dbcontext = PeopleContext:

public class Post 
{ 
    public int PostId { get; set; } 

    [StringLength(50)] 
    [Column(TypeName = "nvarchar")] 
    public string PostName { get; set; } 

    [Column(TypeName = "bit")] 
    public bool Dormant { get; set; } 

    [StringLength(350)] 
    [Column(TypeName = "nvarchar")] 
    public string Description { get; set; } 

    public virtual ICollection<Contract> Contracts { get; set; } 

    public virtual ICollection<Owner> Owners { get; set; } 
} 

我試圖從帖子查找PostName在人民DB從IAR分貝

+2

從s在db.Owners 排序依據s.Post.PostName 這將期待您的數據庫中POST表。如果它不存在,它會拋出這個錯誤,你不得不在 – DevilSuichiro

+0

之間切換上下文感謝有道理,我該怎麼做? –

+1

'Peopledb.Posts.FirstOrDefault(x => x.PostId == t.PostId).PostName'有一天會給你一個'NullReferenceException' ... –

回答

1

顯示業主的名單對我來說,當該消息是很清楚:你是混合兩種情境。這是不可能的。這幾乎意味着創建一個hadhoc dblinq。

的解決方案是:

  • 全球範圍內
  • 通過代碼分離(如果上下文無法全球化):

對我來說,你應該有

public ActionResult OwnerList() 
{        
    var owners = (from s in db.Owners         
       //can't order from here without a dbling/global context 
       select s); 
    //may be a where is missing here ? 

    List<DAOSomeName> viewModel = owners.Select(t => new DAOSomeName 
    { 
     Created = t.Created,    
     Dormant = t.Dormant, 
     OwnerId = t.OwnerId, 
    });// .ToList(); the materialization is done by the following foreach 

    //until here, no run to the db, no data transfered. 

    foreach (DAOSomeName m in viewModel) { 
     m.PostName = Peopledb.Posts.Where(x => x.PostId == t.PostId). 
      Select(x => x.PostName).FirstOrDefault(); 
     //this way you also handle the null case pointed by Trevor 
    } 

    //please note that this way, yout view model is not anymore linked 
    //to the context, except if one property is a navigation property 

    return PartialView("_OwnerList", viewModel.OrderBy(x => x.PostName)); 
} 

public class DAOSomeName { 
    public DateTime Created {get; set;} 
    //Dormant, OwnerId, PostName... 
} 
+0

謝謝,@ tschmit007如何將數據映射到DAOSomName類? –

+0

oups ...我修改我的回覆 – tschmit007

0

通過修改我的控制器到這個:

public ActionResult OwnerList() 
    { 
     var posts = new List<People.Models.Post>(Peopledb.Posts); 
     var owners = new List<Owner>(db.Owners); 
     var ownerposts = (from c in posts 
         join d in owners on c.PostId equals d.PostId 
         orderby c.PostName 
         select new OwnerPost { OwnerId = d.OwnerId, PostName = c.PostName, Created = d.Created, Dormant = d.Dormant }).ToList(); 

     var viewModel = ownerposts.Select(t => new OwnerListViewModel 
     { 
      Created = t.Created, 
      PostName = t.PostName, 
      Dormant = t.Dormant, 
      OwnerId = t.OwnerId, 
     }); 

     return PartialView("_OwnerList", viewModel); 
    } 

並添加OwnerPost類:

public class OwnerPost 
{ 
    public int OwnerId { get; set; } 

    public string PostName { get; set; } 

    public bool Dormant { get; set; } 

    public DateTime Created { get; set; } 
} 

我解決了問題