문제

I have this code for filling DB, but after finish, DB is still empty. Only number in hibernate_unique_key table is higher.

The next thing which is interesting is, that during debugging instances has id, after save, but not like 1,2,3... but very high like 60083, 60084, ... etc. (It not seems normal, because there is only several rows for save).

Before that, there was some problems with references and so on, but now there is no error message or exception.

code is there:

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);
        }
    }
}

I use DB schema genereted by NUnit from Project.Core classes.

Do you have any ideas where i should find problem? I have 2 very similar project which working with no problem. I Copyed this code from one of them and there is practicly no differents (except DB model and so on.)

도움이 되었습니까?

해결책

You need to do your changes in a transaction and commit the transaction afterwards.

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();
}

I advise to look at the SharpArchContrib project wiki, which has some samples on using SharpArch in a windows app/service and how to use Transaction attributes from there.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top