0

1 つのプロパティには多くの写真があります。1 つの写真は 1 つのプロパティに属します。

私の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();

}

4

1 に答える 1

0

私は写真の IEnumrable だけをビューに送信しているので、遅延ロード プロパティの写真コレクションを送信する代わりに、このように写真の IEnumrable を送信しています。

IEnumerable<Photo>photos = session.Query<Photo>().Where(x => x.Property == p).ToList();   
于 2013-03-18T09:39:54.247 に答える