【摘要】Nhibernate中Session的Save与SaveOrUpdate

            DDS.Model.ATest atest = new DDS.Model.ATest();
            atest.AID = Guid.NewGuid();

            ISession session = SessionProvider.GetNewSession();
            using (ITransaction transaction = session.BeginTransaction())
            {
                //session.SaveOrUpdate(atest);
                //上面的代码在事务提交时将导致下面的查询引发:Batch update returned unexpected row count from update; actual row count: 0; expected: 1 
                //在下面的查询时也会导致此错误
session.Save(atest); // 要使用Save int count = session.CreateQuery(" from ATest").List().Count; Response.Write(count); transaction.Commit(); }

 SaveOrUpdate is looking for id value to determine if Save or Update operation should be performed. If you set id to Guid.NewGuid(), and call SaveOrUpdate, an update will be performed, since id is not equal to unsaved-value

参见:http://stackoverflow.com/questions/12329997/nhibernate-query-exception-any-body-help-me?1347102968

你可能感兴趣的:(Hibernate)