C# 获取nhibernate会话内的集合
一个酒店有很多照片。一张照片属于一个财产 在我的mvc控制器中,我得到的是整数的参数数组。这些整数表示我要删除的照片的id 我正在使用nhibernate会话和事务与db进行交互C# 获取nhibernate会话内的集合,c#,hibernate,nhibernate,C#,Hibernate,Nhibernate,一个酒店有很多照片。一张照片属于一个财产 在我的mvc控制器中,我得到的是整数的参数数组。这些整数表示我要删除的照片的id 我正在使用nhibernate会话和事务与db进行交互 public ActionResult DeleteImgs(int[] data) { Property p = null; using (ISession session = ....) { using(ITransaction transaction session.BeginTra
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();
public ActionResult DeleteImgs(int[]数据)
{
属性p=null;
使用(ISession会话=…)
{
使用(ITransaction transaction session.BeginTransaction())
{
Photo Photo=session.Get(数据[0]);
p=session.Get(photo.Id);
//找到图像并删除它们
foreach(数据中的int-id)
{
Photo ph=会话。获取(id);
//从关联中删除属性,以便我可以删除照片
ph.属性=空;
删除(ph);
会话。保存或更新(ph);
}
//立即加载包含剩余照片集合的属性
//这就是问题所在,即使收藏中有照片
//在调试中,我得到的是空集合
p=session.Query()。
.Fetch(x=>x.Photos).ToList()//是否为空?
.FirstOrDefault;
Commit();
}
}
返回视图();
}因为我只发送了一组照片到视图,所以问题就这样解决了, 我没有发送lazy load属性照片集合,而是发送IEnumerable这样的照片
IEnumerable<Photo>photos = session.Query<Photo>().Where(x => x.Property == p).ToList();
IEnumerablephotos=session.Query().Where(x=>x.Property==p.ToList();