none
C# NHibernate Id is not always unique. Help ! RRS feed

  • Question

  • I have a proglem with nhibernate fluent with C# + MVC.
    It doesn't make id unique.

    I run my application.
    Add post. It id is 1.

    I stopped my app. Then I run it again and if i create new record it is generated id 1 for another post and write it over previous.

    public class Post
        {
            public virtual int Id
            { get; set; }

            public virtual string Title
            { get; set; }

            public virtual string ShortDescription
            { get; set; }
            ...
            }
            
     public class PostMap : ClassMap<Post>
        {
            public PostMap()
            {
                Id(x => x.Id);            

                Map(x => x.Title)
                    .Length(500);
    ...                

    public class NHibernateHelper
        {
            private static ISessionFactory _sessionFactory;

            private static ISessionFactory SessionFactory
            {
                get
                {
                    if (_sessionFactory == null)

                        InitializeSessionFactory();
                    return _sessionFactory;
                }
            }

            private static void InitializeSessionFactory()
            {
                _sessionFactory = Fluently.Configure()
                            .Database(MsSqlConfiguration.MsSql2012.ConnectionString(c => c.FromConnectionStringWithKey("DefaultConnection")))                
                            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Post>())
                            .ExposeConfiguration(cfg => new SchemaExport(cfg)
                                                    .Create(true, true))
                    .BuildSessionFactory();            
            }

            public static ISession OpenSession()
            {
                return SessionFactory.OpenSession();
            }
        }
        
    var _blogRepository = new BlogRepository();
    var post = new Post();
    post.Title = "Form den3";
    post.PostedOn = DateTime.Now;
    post.Modified = DateTime.Now;
    var postID = _blogRepository.AddPost(post);

    BlogRepository
    {
    public int AddPost(Post post)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var tran = session.BeginTransaction())
                    {
                        session.Save(post);
                        tran.Commit();
                        return post.Id;
                    }
                }
            }

    My .NET Blog with projects and feedback.

    Since May 30, 2014 I am waiting for Microsoft fix these 2 bug. If you know how to speed them up, please help

    • Moved by Caillen Tuesday, April 21, 2015 2:36 AM
    Saturday, April 11, 2015 2:56 PM

All replies

  • Instead of Save, you should use SaveOrUpdate
    public int AddPost(Post post)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var tran = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(post);
                        tran.Commit();
                        return post.Id;
                    }
                }
            }
    

    Saturday, April 11, 2015 4:13 PM
  • Instead of Save, you should use SaveOrUpdate
    public int AddPost(Post post)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var tran = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(post);
                        tran.Commit();
                        return post.Id;
                    }
                }
            }

    Saveorupdate is used when you want to update the same record if exists and create new if doesn't. The problem reported is different

    Fouad Roumieh

    Sunday, April 12, 2015 7:44 AM