2013-03-18 58 views
0

一個屬性有很多照片。一張照片屬於一個物業。在nhibernate會議內收集藏品

在我的mvc控制器裏,我得到了整數參數數組。這些整數表示我想要刪除的照片的ID。

我正在使用nhibernate會話和事務來與db進行交互。

public ActionResult DeleteImgs(int[] data) 
{ 
    Property p = null; 
    using (ISession session = ....) 
    { 
     using(ITransaction transaction session.BeginTransaction()) 
     {    
     Photo photo = session.Get<Photo>(data[0]); 
     p = session.Get<Property>(photo.Id); 
     // found images and delete them 
     foreach(int id in data) 
     { 
      Photo ph = session.Get<Photo>(id); 
      //remove property from association so I can delete photo 
      ph.Property = null; 
      session.Delete(ph); 
      session.SaveOrUpdate(ph); 
     } 
     //load property now with collection of remaining photos 
     // here IS THE PROBLEM, Even there is photos inside collection 
     // in debug I'm getting empty collection 
     p = session.Query<Property>(). 
      .Fetch(x=>x.Photos).ToList() //empty? 
      .FirstOrDefault; 

     transaction.Commit(); 
     } 
    } 
    return View(); 

}

回答

0

由於我只發送IEnumrable的照片到視圖問題就解決了這個樣子, ,而不是發送懶負荷特性的照片集,我喜歡這個

發送照片的IEnumerable
IEnumerable<Photo>photos = session.Query<Photo>().Where(x => x.Property == p).ToList();