Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3356122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:28:38+00:00 2026-05-18T02:28:38+00:00

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

  • 0

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_AircraftIsPersisted()
–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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:28:38+00:00Added an answer on May 18, 2026 at 2:28 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.