PLZ HELP!!!Not able to Delete the records from DB using NHibernate
-
Hi, I am using NHibernate to persist my objects. To delete an object from DB I have written the below code : I have checked my DB too and it has records. public void Delete(T item) { try { using (ISession session = factory.OpenSession()) { using (session.BeginTransaction()) { session.Delete(item); session.Transaction.Commit(); //Code is breaking here. } } } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); System.Windows.Forms.MessageBox.Show(e.StackTrace); } } The code is breaking at line session.transaction.commit(); with following error message "Unexpected row count: 0; expected: 1" Please let me know how to fix this. Thanks in Advance Regards Puneet
-
Hi, I am using NHibernate to persist my objects. To delete an object from DB I have written the below code : I have checked my DB too and it has records. public void Delete(T item) { try { using (ISession session = factory.OpenSession()) { using (session.BeginTransaction()) { session.Delete(item); session.Transaction.Commit(); //Code is breaking here. } } } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); System.Windows.Forms.MessageBox.Show(e.StackTrace); } } The code is breaking at line session.transaction.commit(); with following error message "Unexpected row count: 0; expected: 1" Please let me know how to fix this. Thanks in Advance Regards Puneet
Hi, long time that I did something with hibernate, but I think the problem is, that the item you want to delete is associated with another session. Try to refresh the item with the new session before you delete it (this is not the best way to do this, it is just a quick-and-dirty solution).
...
session.Refresh(item);
session.Delete(item);
...Please let me know if this fixed your problem. Regards Sebastian