In my eyes there is nothing special about my code (see below). 
I am using FluentNHibernate for mapping. Could this be the problem?
I also tried to set the RowVersion explicitly as you did in your second 
example. But this did not change anything.

When creating the db 'code first', RowVersion is created as not nullable in 
the db, even though it is declared as nullable in the entity resp. the 
mapping.

If it helps I can provide my sample application. It was too large for this 
forum (6 MB including NH dlls). 

Entity:

public class Sample
{
    public virtual int Id { get; protected set; }
    public virtual Nullable<int> RowVersion { get; protected set; } // => 
NullReferenceException
    //public virtual int RowVersion { get; protected set; }         // => 
StaleObjectStateException
    public virtual string Name { get; set; }
}
Map:

public class MapSample : ClassMap<Sample>
{
    public MapSample()
    {
        Table("Sample");
        Id(x => x.Id)
            .GeneratedBy.HiLo("1000")
            .UnsavedValue(-1);
        Version(x => x.RowVersion);
        Map(x => x.Name);
    }
}
SessionFactory:

public static class SessionFactory
{
    private static readonly ISessionFactory _sessionfactory;
    private static readonly Configuration _configuration;
 
    /// <summary>
    /// Static constructor that creates a new session factory used by all 
instances of the SessionManager.
    /// </summary>
    static SessionFactory()
    {
        try
        {
            _configuration =
                Fluently
                    .Configure()
                    .Database(MsSqlConfiguration
                        .MsSql2012
                        .ConnectionString(connstr => 
connstr.FromConnectionStringWithKey("nh4test"))
                        .DefaultSchema("dbo"))
                        .Mappings(m =>
                        {
                            m.FluentMappings.AddFromAssemblyOf<Sample>();
                        })
                    .BuildConfiguration();
            _sessionfactory = _configuration.BuildSessionFactory();
        }
        catch (Exception err)
        {
            throw new Exception("Building SessionFactory failed", err);
        }
    }
 
    public static Configuration GetConfiguration()
    {
        return _configuration;
    }
 
    public static ISessionFactory GetSessionFactory()
    {
        return _sessionfactory;
    }
 
    public static ISession OpenSession()
    {
        return _sessionfactory.OpenSession();
    }
 
    public static IStatelessSession OpenStatelessSession()
    {
        return _sessionfactory.OpenStatelessSession();
    }
}
Program:

using (var session = SessionFactory.OpenSession())
{
    using (var tx = session.BeginTransaction())
    {
        var sample = new Sample
        {
            Name = "Sample";
        };
        session.SaveOrUpdate(sample);
        tx.Commit(); // => Exception
    }
}

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to