使用Session.Save通过NHibernate 4使用map-by-code插入SQL Server数据库

时间:2017-11-01 03:04:34

标签: nhibernate nhibernate-mapping mapping-by-code

目前遇到Session.Save()没有插入记录并产生以下异常的问题:

        null id in NHModels.Domain.Activity entry (don't flush the Session after an exception occurs)

at NHibernate.Event.Default.DefaultFlushEntityEventListener.CheckId(Object obj, IEntityPersister persister, Object id, EntityMode entityMode)
at NHibernate.Event.Default.DefaultFlushEntityEventListener.GetValues(Object entity, EntityEntry entry, EntityMode entityMode, Boolean mightBeDirty, ISessionImplementor session)
at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent event)
at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent event)
at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event)
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
at NHUnitOfWork.Dispose() in NHUnitOfWork.cs:line
at DatabaseActivityOperations.<WriteActivity>d__6.MoveNext() in DatabaseActivityOperations.cs:line 240

我的类和映射看起来像这样。

活动(为简单起见,我已删除了几个与此课程相关的IList)

public class Activity {

    public Activity() {
        Activityschema = new List<ActivitySchema>();
    }

    public virtual int ActivityKey { get; set; }
    public virtual string Activityname { get; set; }
    public virtual string Activitydescription { get; set; }
    public virtual DateTime Averageactivitytime { get; set; }
    public virtual int Averagenumberpeople { get; set; }
    public virtual string Worktype { get; set; }
    public virtual bool? Canautocomplete { get; set; }
    public virtual IList<ActivitySchema> Activityschema { get; set; }
}

ActivityMap

public class ActivityMap : ClassMapping<Activity> {

    public ActivityMap() {
        Schema("dbo");
        Lazy(true);
        Id(x => x.ActivityKey, map => { map.Generator(Generators.Identity); });
        Property(x => x.Activityname, map => { map.NotNullable(true); map.Length(50); });
        Property(x => x.Activitydescription, map => { map.NotNullable(true); map.Length(100); });
        Property(x => x.Averageactivitytime, map =>
        {
            map.NotNullable(true);
            map.Type(NHibernateUtil.Time);
        });
        Property(x => x.Averagenumberpeople, map => { map.NotNullable(true); map.Precision(10); });
        Property(x => x.Worktype, map => { map.NotNullable(true); map.Length(50); });
        Property(x => x.Canautocomplete);
        Bag(x => x.Activityschema, colmap =>  { colmap.Key(x => x.Column("ActivityKey")); colmap.Inverse(true); }, map => { map.OneToMany(); }); 
    }
}

最后,这是我的工作单元课程:

public class NHUnitOfWork : IDisposable
{
    public static string ConnectingString { get; private set; } = @"data source=nh;initial catalog=db;MultipleActiveResultSets=True;";
    protected static Configuration _config;
    protected static NHibernate.ISessionFactory _sessionFactory;
    public NHibernate.ISession Session { get; private set; }
    protected NHibernate.ITransaction Transaction { get; set; }
    private const System.Data.IsolationLevel ISOLATION_LEVEL = System.Data.IsolationLevel.ReadUncommitted;
    private bool RollBack { get; set; } = false;

    public NHUnitOfWork(string databaseConnectionString)
    {
        if (_config == null)
        {
            var cfg = new Configuration();
            cfg.DataBaseIntegration(db =>
            {
                db.Driver<NHibernate.Driver.SqlClientDriver>();
                db.ConnectionString = @"data source=nh;initial catalog=db;MultipleActiveResultSets=True;";
                //db.ConnectionString = databaseConnectionString;
                db.Dialect<MsSql2012Dialect>();
                db.BatchSize = 500;
            })
            .AddAssembly(typeof(Activity).Assembly)
            .SessionFactory()
            .GenerateStatistics();

            var mapper = new ModelMapper();
            mapper.AddMappings(typeof(ActivityMap).Assembly.GetTypes());
            cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
            _config = cfg;
            _sessionFactory = _config.BuildSessionFactory();
        }
        Session = _sessionFactory.OpenSession();
        Transaction = Session.BeginTransaction(ISOLATION_LEVEL);
        RollBack = false;
    }

    public void Commit()
    {
        Transaction.Commit();
    }

    public void Rollback()
    {
        if (Transaction.IsActive) Transaction.Rollback();
    }

    public void Dispose()
    {
        if (RollBack)
        {
            Transaction.Rollback();
        }
        else
        {
            Transaction.Commit();
        }

        Session.Close();
    }
}

到目前为止,我相信我的配置是正确的。我成功地使用Session.Query来读取数据并且不会出现问题。当我写这样的东西来添加新记录时,问题出现了:

    var activity = new Activity
    {
        Activityname = "TestActivity",
        Activitydescription = "This is a test",
        Averagenumberpeople = 1,
        Worktype = "Test",
        Canautocomplete = false,
        Averageactivitytime = new DateTime(1, 1, 1, 0, 55, 55)
    };
    using (var uow = new NHUnitOfWork(NHUnitOfWork.ConnectingString))
    {
        uow.Session.Save(activity); // Produces exception here

        //This also produces an exception
        //uow.Session.Save(activity, Generators.Identity);
    }

我认为这与我在ActivityMap中映射ID的方式有关,而且生成器没有按预期工作。我尝试将其更改为其他几种类型并获得相同的异常,或者说它无法转换为SystemInt32。我也尝试将ID更改为long并指定数据类型,但没有运气。在这里我似乎做错了什么?

1 个答案:

答案 0 :(得分:0)

我发布此消息后几分钟,我发现问题实际上与我如何设置日期时间有关。

new DateTime(1, 1, 1, 0, 55, 55)

它不喜欢“1/1/0001”部分,所以这似乎引起了问题,我只关心时间片。将年份更改为像2001这样的东西修复了插件并且工作正常,消息不是很具描述性。