NHibernate增量列不是id

时间:2011-01-18 19:59:10

标签: nhibernate auto-increment

我希望db为我自动增加一个EmployeeId#。这将是一个业务ID,与db id(生成hilo)不同。我发现它看起来像promising solution但是在实现它时遇到了问题。

我无法判断我得到的异常是因为映射不正确,SQLite无法处理它,或者其他什么。我还想知道是否有人对此有不同的解决方案。

干杯,
浆果

MAPPING

<class name="Resource" table="Resources" ...>

<id name="Id" type="System.Int32" unsaved-value="0">
  <column name="ResourceId" />
  <generator class="hilo" />
</id>
...
  <component name="EmployeeNumber" unique="true">
    <property name="StorageValue" generated="always" insert="false">
      <column name="EmployeeNumber" />
    </property>
  </component>
  ...
 </class>

 <database-object>
<create>
  ALTER TABLE Entity DROP COLUMN EmployeeNumber
  ALTER TABLE Entity ADD EmployeeNumber INT IDENTITY
</create>
<drop>
  ALTER TABLE Entity DROP COLUMN EmployeeNumber
</drop>
</database-object>

EXCEPTION输出

ALTER TABLE Entity DROP COLUMN EmployeeNumber
  ALTER TABLE Entity ADD EmployeeNumber INT IDENTITY

ALTER TABLE Entity DROP COLUMN EmployeeNumber
  ALTER TABLE Entity ADD EmployeeNumber INT IDENTITY
failed: System.Exception : Generate schema error: 
----> System.Data.SQLite.SQLiteException : SQLite error
near "DROP": syntax error
NHibernate\DbConfiguration\NHibInitialization\SchemaManager.cs(58,0): at Smack.Core.Data.NHibernate.DbConfiguration.NHibInitialization.SchemaManager._generateNewDb(Configuration cfg, IDbConnection conn)
NHibernate\DbConfiguration\NHibInitialization\SchemaManager.cs(16,0): at Smack.Core.Data.NHibernate.DbConfiguration.NHibInitialization.SchemaManager.GenerateNewDb(Configuration cfg, IDbConnection connection)
NHibernate\TestFixtures\SQLite\SQLiteBaseTestFixture.cs(56,0): at Smack.Core.TestingSupport.NHibernate.TestFixtures.SQLite.SQLiteBaseTestFixture._buildSchema()
NHibernate\TestFixtures\SQLite\SQLiteBaseTestFixture.cs(37,0): at Smack.Core.TestingSupport.NHibernate.TestFixtures.SQLite.SQLiteBaseTestFixture._SetUpNHibernateSession()
NHibernate\TestFixtures\SQLite\SQLiteBaseTestFixture.cs(25,0): at Smack.Core.TestingSupport.NHibernate.TestFixtures.SQLite.SQLiteBaseTestFixture.OnSetUp()
Mappings\EmployeeMappingTests.cs(23,0): at Smack.ConstructionAdmin.Data.Tests.Mappings.EmployeeMappingTests.OnSetUp()
BaseTestFixture.cs(27,0): at Smack.Core.TestingSupport.BaseTestFixture.SetUp()
--SQLiteException
at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain)
at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()

1 个答案:

答案 0 :(得分:1)

在ALTER TABLE SQLite语句here的流程图中,我找不到DROP子句。您只能添加和重命名列。因此,您可以将EmployeeNumber重命名为EmployeeNumber_old而不是删除它。您也可以删除并重新创建整个表(但如果这是一个包含生产数据的持久SQLite DB,我会反对它)