错误信息:
---------------------------
---------------------------
NHibernate.Exceptions.GenericADOException: could not get or update next value[SQL: ] ---> System.Data.SqlClient.SqlException: 对象名 'hibernate_unique_key' 无效。
在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
在 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
在 System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
在 System.Data.SqlClient.SqlDataReader.get_MetaData()
在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
在 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
在 System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
在 NHibernate.Id.TableGenerator.DoWorkInCurrentTransaction(ISessionImplementor session, IDbConnection conn, IDbTransaction transaction)
在 NHibernate.Engine.TransactionHelper.Work.DoWork(IDbConnection connection, IDbTransaction transaction)
--- 内部异常堆栈跟踪的结尾 ---
在 NHibernate.Engine.TransactionHelper.Work.DoWork(IDbConnection connection, IDbTransaction transaction)
在 NHibernate.Transaction.AdoNetTransactionFactory.ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork work, Boolean transacted)
在 NHibernate.Transaction.AdoNetWithDistributedTransactionFactory.ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork work, Boolean transacted)
在 NHibernate.Engine.Transaction.Isolater.DoIsolatedWork(IIsolatedWork work, ISessionImplementor session)
在 NHibernate.Engine.TransactionHelper.DoWorkInNewTransaction(ISessionImplementor session)
在 NHibernate.Id.TableGenerator.Generate(ISessionImplementor session, Object obj)
在 NHibernate.Id.TableHiLoGenerator.Generate(ISessionImplementor session, Object obj)
在 NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
在 NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
在 NHibernate.Impl.SessionImpl.Save(Object obj)
在 WindowsFormsApplication1.Form1.Form1_Load(Object sender, EventArgs e) 位置 C:\Users\JianKunKing\Desktop\NHibernate\NHibernate03\WindowsFormsApplication1\Form1.cs:行号 41
---------------------------
确定
---------------------------
解决方法:
I’ve genereated a schema for my (SQL 2005) db using SchemaExport, and it’s created a table
CREATE TABLE [dbo].[hibernate_unique_key](
[next_hi][int] NULL
) ON [PRIMARY]
When I try to add an entity, I get the error:
---------------------------
---------------------------
NHibernate.Id.IdentifierGenerationException: could not read a hi value - you need to populate the table: hibernate_unique_key
在 NHibernate.Id.TableGenerator.DoWorkInCurrentTransaction(ISessionImplementor session, IDbConnection conn, IDbTransaction transaction)
在 NHibernate.Engine.TransactionHelper.Work.DoWork(IDbConnection connection, IDbTransaction transaction)
在 NHibernate.Transaction.AdoNetTransactionFactory.ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork work, Boolean transacted)
在 NHibernate.Transaction.AdoNetWithDistributedTransactionFactory.ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork work, Boolean transacted)
在 NHibernate.Engine.Transaction.Isolater.DoIsolatedWork(IIsolatedWork work, ISessionImplementor session)
在 NHibernate.Engine.TransactionHelper.DoWorkInNewTransaction(ISessionImplementor session)
在 NHibernate.Id.TableGenerator.Generate(ISessionImplementor session, Object obj)
在 NHibernate.Id.TableHiLoGenerator.Generate(ISessionImplementor session, Object obj)
在 NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
在 NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
在 NHibernate.Impl.SessionImpl.Save(Object obj)
在 WindowsFormsApplication1.Form1.Form1_Load(Object sender, EventArgs e) 位置 C:\Users\JianKunKing\Desktop\NHibernate\NHibernate03\WindowsFormsApplication1\Form1.cs:行号 42
---------------------------
确定
---------------------------
Solution:
NHibernate expects to find a value that stores the current hi value in that table, ie it first runs something like:
current_hi =[SELECT max(next_hi) FROM hibernate_unique_key].
So all you need to do is seed that table with an initial number, ie:
INSERT INTO hibernate_unique_key(next_hi) VALUES (0)