开发者

Fluent NHibernate to an in-memory SQLite DB throws exceptions on insert with Version

I've a rather large domain-model that persists using fluent NHibernate to MS SQL Server 2008.

Recently we've been running into concurrency problems, so I implemented Versioning NHibernate's excellent Version with an eye to using MSSQL's timestampdatatype. All my entities subclass the cunningly named DomainEntityabstract class, which has the following IAutoMappingOverride:

public class DomainEntityOverride : IAutoMappingOverride<DomainEntity> {
    public void Override(AutoMapping<DomainEntity> mapping) {
        mapping.OptimisticLock.Version();
        mapping.Version(entity => entity.Version);
    }
}

Furthermore I set up the versioning using an IVersionConvention:

public class VersionConvention : IVersionConvention {
    public void Apply(IVersionInstance instance) {
        instance.Column("Version");
        instance.Generated.Always();
        instance.UnsavedValue("null");
        instance.Not.Nullable();
        instance.CustomSqlType("timestamp");
    }
}

My understanding is that this should cause the Version to always be generated, and checked for concurrency issues. In my dev and production environments (both using MSSQL2008) it seems to work, but my in-memory tests using SQLite no longer work.

A basic test that now fails is the following:

    [Test(Description = "Can save an aircraft and it is persisted")]
    public void PersistAircraft_NewAircraft_AircraftIsPersisted() {
        var id = saveEntity(_aircraft); //exception thrown here

        var persisted = getEntity<Aircraft>(id);

        Assert.That(persisted.Registration, Is.EqualTo(_aircraftRegistration));
        Assert.That(persisted.Remarks, Is.EqualTo(_aircraftRemark));
    }

I get the follwing exception:

Test 'NOIS.Persistence.Tests.InMemory.AircraftPersistenceTest.PersistAircraft_NewAircraft_AircraftIsPersisted' failed: NHibernate.Exceptions.GenericADOException : could not insert: [NOIS.Model.Entities.AircraftType#9c2809ea-c0c5-4778-838b-9e2500a6d2ef][SQL: INSERT INTO "AircraftType" (ProducedBy, Active, Remarks, Name, Code, Category_id, Id) VALUES (?, ?, ?, ?, ?, ?, ?)]
----> System.Data.SQLite.SQLiteException : Abort due to constraint violation
AircraftType.Version may not be NULL

at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) at NHibernate.Action.EntityInsertAction.Execute() at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) at NHibernate.Engine.ActionQueue.ExecuteActions() at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) at NHibernate.Impl.SessionImpl.Flush() at NHibernate.Transaction.AdoTransaction.Commit() FluentlyConfiguredInMemoryDatabaseTest.cs(49,0): at NOIS.Persistence.Tests.FluentlyConfiguredInMemoryDatabaseTest.saveEntity(DomainEntity entity) InMemory\AircraftPersistenceTest.cs(33,0): at NOIS.Persistence.Tests.InMemory.AircraftPersistenceTest.PersistAircraft_NewAircraft_AircraftIsP开发者_如何学编程ersisted() --SQLiteException at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt) at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt) 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() at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation) at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)


I'm not sure if I fully understand what you are doing here.

But as I understand, you are using an SQL Server feature, this timestamps, which are not available in Sqlite. So it doesn't work there.

As far as I know, timestamps are set by Sql Server when inserting or updating records. They are not under control of NH. But NH supports them, with the trade off that it needs to get its value back from the database after inserting or updating. You need a good reason to use timestamps with NHibernate.

You don't need this SqlServer stuff to get NH's optimistic locking mechanism to work. It is actually fully managed by NH and doesn't require anything special from the database. Just define a integer column and property and map it as the entities version. Everything else is done be NH. I never had to use anything else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜