Generator class should be equals to identity rather than increment. 2011/3/20, SO King Fai <[email protected]>: > I am a very newbie on NH. I am going throw the tutorial "Your first > NHibernate based application" and try to modify it to my own code > standard. > Such as try to connect to MSSQL instead of MSSQL CE, > Change Database Entity > Change the PK to Auto-Increment > > The result is that I can select record from Database successfully. > However, I have got the Following Error when try to commit the new > record > But if i use Debug mode... I found that the NH already can get the new > EnterpriseID of the inserted record to my local object. > > I have tried to Google my problem but seems strongly related result > for me..... > > Can anyone point out what is the problem??? > Thank you very much > > =====The Following is the Detailed Error and Other related > Information==== > NHibernate.Exceptions.GenericADOException was unhandled > Message="could not execute batch command.[SQL: SQL not available]" > Source="NHibernate" > SqlString="SQL not available" > StackTrace: > at > NHibernate.AdoNet.SqlClientBatchingBatcher.DoExecuteBatch(IDbCommand > ps) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\AdoNet > \SqlClientBatchingBatcher.cs:line 95 > at > NHibernate.AdoNet.AbstractBatcher.ExecuteBatchWithTiming(IDbCommand > ps) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\AdoNet > \AbstractBatcher.cs:line 431 > at NHibernate.AdoNet.AbstractBatcher.ExecuteBatch() in d:\CSharp > \NH\NH\nhibernate\src\NHibernate\AdoNet\AbstractBatcher.cs:line 416 > at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in > d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:line > 129 > at NHibernate.Engine.ActionQueue.ExecuteActions() in d:\CSharp > \NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:line 169 > at > NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource > session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default > \AbstractFlushingEventListener.cs:line 249 > at > NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent > event) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default > \DefaultFlushEventListener.cs:line 19 > at NHibernate.Impl.SessionImpl.Flush() in d:\CSharp\NH\NH > \nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 1472 > at NHibernate.Transaction.AdoTransaction.Commit() in d:\CSharp > \NH\NH\nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:line > 187 > at TryNHibernate.Form1.button1_Click(Object sender, EventArgs > e) in C:\Users\Felix\Documents\Visual Studio 2008\Projects > \TryNHibernate\TryNHibernate\Form1.cs:line 41 > at System.Windows.Forms.Control.OnClick(EventArgs e) > at System.Windows.Forms.Button.OnClick(EventArgs e) > at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) > at System.Windows.Forms.Control.WmMouseUp(Message& m, > MouseButtons button, Int32 clicks) > at System.Windows.Forms.Control.WndProc(Message& m) > at System.Windows.Forms.ButtonBase.WndProc(Message& m) > at System.Windows.Forms.Button.WndProc(Message& m) > at > System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) > at > System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) > at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr > hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) > at > System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) > at > System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 > dwComponentID, Int32 reason, Int32 pvLoopData) > at > System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 > reason, ApplicationContext context) > at > System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 > reason, ApplicationContext context) > at System.Windows.Forms.Application.Run(Form mainForm) > at TryNHibernate.Program.Main() in C:\Users\Felix\Documents > \Visual Studio 2008\Projects\TryNHibernate\TryNHibernate > \Program.cs:line 18 > at System.AppDomain._nExecuteAssembly(Assembly assembly, > String[] args) > at System.AppDomain.ExecuteAssembly(String assemblyFile, > Evidence assemblySecurity, String[] args) > at > Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() > at System.Threading.ThreadHelper.ThreadStart_Context(Object > state) > at System.Threading.ExecutionContext.Run(ExecutionContext > executionContext, ContextCallback callback, Object state) > at System.Threading.ThreadHelper.ThreadStart() > InnerException: System.Data.SqlClient.SqlException > Message="Cannot insert explicit value for identity column in > table 'Enterprise' when IDENTITY_INSERT is set to OFF." > Source=".Net SqlClient Data Provider" > ErrorCode=-2146232060 > Class=16 > LineNumber=1 > Number=544 > Procedure="" > Server="FELIX-WIN7PC\\SQLEXPRESS" > State=1 > StackTrace: > at > System.Data.SqlClient.SqlConnection.OnError(SqlException exception, > Boolean breakConnection) > at > System.Data.SqlClient.SqlInternalConnection.OnError(SqlException > exception, Boolean breakConnection) > at > System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject > stateObj) > at System.Data.SqlClient.TdsParser.Run(RunBehavior > runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, > BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject > stateObj) > at > System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, > RunBehavior runBehavior, String resetOptionsString) > at > System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior > cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean > async) > at > System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior > cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String > method, DbAsyncResult result) > at > System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult > result, String methodName, Boolean sendToPipe) > at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() > at > System.Data.SqlClient.SqlCommand.ExecuteBatchRPCCommand() > at System.Data.SqlClient.SqlCommandSet.ExecuteNonQuery() > at > NHibernate.AdoNet.SqlClientSqlCommandSet.ExecuteNonQuery() in d:\CSharp > \NH\NH\nhibernate\src\NHibernate\AdoNet\SqlClientSqlCommandSet.cs:line > 117 > at > NHibernate.AdoNet.SqlClientBatchingBatcher.DoExecuteBatch(IDbCommand > ps) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\AdoNet > \SqlClientBatchingBatcher.cs:line 91 > InnerException: > > The Following is the Code of the Application around the error: > Enterprise product = new Enterprise { enterpriseid = > 10L, > enterprisename = "name", loginname = "user", password = "user" }; > using (ISession session = > NHibernateHelper.OpenSession()) > using (ITransaction transaction = > session.BeginTransaction()) > { > session.Save(product); > transaction.Commit(); > } > > using (ISession session = _sessionFactory.OpenSession()) > { > Enterprise fromDb = session.Get<Enterprise>(0L); > // Test that the product was successfully > inserted > Assert.IsNotNull(fromDb); > MessageBox.Show(fromDb.enterprisename); > } > > It is the Mapping XML > <?xml version="1.0" encoding="utf-8" ?> > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" > assembly="TryNHibernate" > namespace="TryNHibernate.Domain"> > > <class name="Enterprise"> > <id name="enterpriseid"> > <generator class="increment" /> > </id> > <property name="enterprisename" /> > <property name="loginname" /> > <property name="password" /> > </class> > > <!-- more mapping info here --> > > </hibernate-mapping> > > Database Definition: > Table Enterprise > Numeric(10, 0) EnterpriseID Identity (0,1) PK > nvarchar(MAX) EnterpriseName > nvarchar(MAX) LoginName > nvarchar(MAX) password > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > >
-- Enviado desde mi dispositivo móvil -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
