SharpArch.Data.NHibernate.Repository.SaveOrUpdate()不保存

时间:2012-04-17 09:48:52

标签: nhibernate fluent-nhibernate s#arp-architecture

我有这个代码用于填充DB,但在完成后,DB仍然是空的。只有hibernate_unique_key表中的数字更高。

接下来有趣的是,在调试实例期间有id,保存之后,但不像1,2,3 ......但非常高,如60083,60084,...等等(这似乎不正常,因为只有几行可以保存。

在此之前,引用存在一些问题等等,但现在没有错误消息或异常。

代码在那里:

using System;
using NHibernate.Cfg;
using SharpArch.Data.NHibernate;
using LaboratorniMetody.Data.NHibernateMaps;
using SharpArch.Core.PersistenceSupport;
using LaboratorniMetody.Core;

namespace LaboratorniMetody.FillSchema
{
    class Program
    {
        private static Configuration configuration;

        static void Main(string[] args)
        {
            configuration = NHibernateSession.Init(new SimpleSessionStorage(), new String[] { "LaboratorniMetody.Data" },
                                            new AutoPersistenceModelGenerator().Generate(),
                                            "../../../../app/LaboratorniMetody.Web/NHibernate.config");

            IRepository<Laboratory> LaboratoryRepository = new Repository<Laboratory>();
            Laboratory Laboratory1 = new Laboratory() { Name = "Hematologie" };//Method
            Laboratory l = LaboratoryRepository.SaveOrUpdate(Laboratory1);
            Laboratory Laboratory2 = new Laboratory() { Name = "Patologie" };//Method
            LaboratoryRepository.SaveOrUpdate(Laboratory2);
            Laboratory Laboratory3 = new Laboratory() { Name = "Laboratoře" };//Method
            LaboratoryRepository.SaveOrUpdate(Laboratory3);
        }
    }
}

我使用来自Project.Core类的NUnit生成的数据库模式。

你有什么想法我应该找到问题吗?我有2个非常相似的项目,没有问题。我从其中一个复制了这个代码,实际上没有不同的东西(除了DB模型等等。)

1 个答案:

答案 0 :(得分:1)

您需要在事务中进行更改并在事后提交事务。

using(var tx = NHibernateSession.Current.BeginTransaction())
{
   IRepository<Laboratory> LaboratoryRepository = new Repository<Laboratory>();
   Laboratory Laboratory1 = new Laboratory() { Name = "Hematologie" };//Method
   Laboratory l = LaboratoryRepository.SaveOrUpdate(Laboratory1);
   Laboratory Laboratory2 = new Laboratory() { Name = "Patologie" };//Method
   LaboratoryRepository.SaveOrUpdate(Laboratory2);
   Laboratory Laboratory3 = new Laboratory() { Name = "Laboratoře" };//Method
   LaboratoryRepository.SaveOrUpdate(Laboratory3);

   tx.Commit();
}

我建议查看SharpArchContrib项目wiki,其中包含一些在Windows应用程序/服务中使用SharpArch以及如何使用其中的Transaction属性的示例。

https://github.com/sharparchitecture/Sharp-Architecture-Contrib/wiki/