我一直在为初学者阅读nhibernate 3,并发现我不应该通过MS Sql 2008使用自动ID,因为这会破坏工作单元(我正在实现的目的)。
在书中他们似乎推荐希洛,但我仍然没有真正得到它们以及它们如何保持一切独特。从我读到的内容看起来像id中的巨大空白,你可能需要更大的数据类型来防止它使用所有的组合(我目前正在使用int)。
所以我看到的其他建议是GUID什么不会有这个问题,但是有点难以阅读(如果你必须在数据库中手动比较一些东西)并且需要更多的空间。
所以现在我只是想做一个示例项目,看看hilo是如何工作的。
我有一个包含2个字段的简单表(一个pk和一个varchar)
public class HiLo
{
public virtual int Id { get; set; // not sure if this should be a private set. I see it both ways }
public virtual string Title { get; set; }
}
public class HiLoMapping : ClassMap<HiLo>
{
public HiLoMapping()
{
Id(x => x.Id).GeneratedBy.HiLo("100");
Map(x => x.Title);
}
}
不确定100实际意味着什么,但它不起作用。
NHibernate.Exceptions.GenericADOException was caught
Message=could not get or update next value[SQL: ]
Source=NHibernate
StackTrace:
at NHibernate.Engine.TransactionHelper.Work.DoWork(IDbConnection connection, IDbTransaction transaction)
at NHibernate.Transaction.AdoNetTransactionFactory.ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork work, Boolean transacted)
at NHibernate.Transaction.AdoNetWithDistributedTransactionFactory.ExecuteWorkInIsolation(ISessionImplementor session, IIsolatedWork work, Boolean transacted)
at NHibernate.Engine.Transaction.Isolater.DoIsolatedWork(IIsolatedWork work, ISessionImplementor session)
at NHibernate.Engine.TransactionHelper.DoWorkInNewTransaction(ISessionImplementor session)
at NHibernate.Id.TableGenerator.Generate(ISessionImplementor session, Object obj)
at NHibernate.Id.TableHiLoGenerator.Generate(ISessionImplementor session, Object obj)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at unitofwork.Models.Repository.StoreRepo.Create(HiLo hilo) in ]StoreRepo.cs:line 32
at unitofwork.Models.Service.StoreService.CreateStore() inStoreService.cs:line 33
InnerException: System.Data.SqlClient.SqlException
Message=Invalid object name 'hibernate_unique_key'.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=208
Procedure=""
Server=(local)
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()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
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.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
at NHibernate.Id.TableGenerator.DoWorkInCurrentTransaction(ISessionImplementor session, IDbConnection conn, IDbTransaction transaction)
at NHibernate.Engine.TransactionHelper.Work.DoWork(IDbConnection connection, IDbTransaction transaction)
InnerException:
现在在我的数据库中我有3个表(2个有自动id,1个有hilo)。 hilo表有点漂浮在其他2个表之间有关系。
答案 0 :(得分:1)
对于HiLo的解释,我读过的最简单,最简洁的是What's the Hi/Lo algorithm?。
至于你的例子不工作,你错过了hibernate_unique_key表(默认名为Hilo表)。
解决它的最简单方法是让NHibernate为你创建它 - 将Nhibernate配置中的SchemaAutoAction属性设置为'Update'。
答案 1 :(得分:1)
您需要创建HiLo表check it here
我通常使用Guid.Comb,因为它对我来说比较容易,因为我通常与另一个不了解HiLo的系统共享同一个数据库