Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate not saving entity

I've got a project where I'm moving data from database to another. I've got several tables working but the current one isn't. When calling the Session.Save(entity) nothing appears to occur (no insert record being sent from NHibernate). The entities that aren't saving are the Configuration ones in code below. I'm included other code related to another entity which is working fine (about a dozen are). Data is going from an Access database to a MSSQL database.

Code that copies object and performs the session save. Nothing is inserted when save is called on Configuration entity.

public void Save(Entities.Access.CompTool o)
{
    var n = new CompTool();
    n.Name = o.Name;
    n.Description = o.Description;
    n.DefaultLocation = o.DefaultLocation;
    n.DateModified = o.DateModified;
    n.OldId = o.Id;

    GetSession().Save(n);
}

public void Save(Entities.Access.Configuration o)
{
    var n = new Configuration();
    n.Name = o.Name;
    n.Description = o.Description;
    n.Value = o.Value;

    GetSession().Save(n);
}

Mapping Configuration

using TestProg.DatabaseConverter.Entities.Sql;
using FluentNHibernate.Mapping;

namespace TestProg.DatabaseConverter.Mappings.Sql
{
    public class ConfigurationMap : ClassMap<Configuration>
    {
        public ConfigurationMap()
        {
            Table("Configuration");
            Id(x => x.Name).GeneratedBy.Assigned();
            Map(x => x.Value);
            Map(x => x.Description).Column("Desription");
        }
    }
}

Mapping CompTool

using TestProg.DatabaseConverter.Entities.Sql;
using FluentNHibernate.Mapping;

namespace TestProg.DatabaseConverter.Mappings.Sql
{
    public class CompToolMap: ClassMap<CompTool>
    {
        public CompToolMap()
        {
            Table("CompTools");
            Id(x => x.Id).Column("ID");
            Map(x => x.Name);
            Map(x => x.Description);
            Map(x => x.DefaultLocation);
            Map(x => x.DateModified);
            Map(x => x.OldId);
        }
    }
}

Configuration entity

using System;

namespace TestProg.DatabaseConverter.Entities.Sql
{
    public class Configuration
    {
        public virtual string Name { get; set; }
        public virtual string Value { get; set; }
        public virtual string Description { get; set; }
    }
}

Code to create Configuration table:

CREATE TABLE Configuration
(
    Name nvarchar(50) PRIMARY KEY,
    Value nvarchar(50) DEFAULT '',
    Desription nvarchar(100) DEFAULT ''
)
like image 520
Mark E Avatar asked Oct 26 '15 17:10

Mark E


1 Answers

The session.Save(...) does not mean SQL INSERT.

The instance of NHibernate session represents a context / unit of work, which does (or even hides) the DB manipulation.

DB WRITE operations are done in batches, if needed or if we explicitly ask for. And the way how we can force WRITE operation si by calling

session.Flush();

Check these parts of documentation (well all of them are helpful, but these cover session and its Flush mode)

2.3. Contextual Sessions

and

9.6. Flush

From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection's state with the state of objects held in memory.

  • from some invocations of Find() or Enumerable()
  • from NHibernate.ITransaction.Commit()
  • from ISession.Flush()
like image 76
Radim Köhler Avatar answered Oct 20 '22 11:10

Radim Köhler