[英]Fetching collection inside nhibernate session
一間酒店有很多照片。 一張照片屬於一項財產。
在我的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();
}
由於我只向視圖發送了IEnumrable照片,因此像這樣解決了問題,而不是像這樣發送延遲加載屬性照片集合,而是發送了IEnumerable of Photos
IEnumerable<Photo>photos = session.Query<Photo>().Where(x => x.Property == p).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.