Basic Fluent NHibernate program compiles and runs, but nothing is saved to or loaded from database

I am trying a very simple Fluent Nhibernate example: SQL 2005 database with one table, VS2008 console application. The table has one record before the program starts.

I am trying to add one record, then display all records from table. Programs successfully compiles and runs without any exceptions, however no records are displayed. HBM mapping file is not created either. It looks like that the program totally ignores the database (although it connects to it).

Here is my code - I tried to keep it minimal:

Entity:

namespace FluentNhibernationConsole.Entities
{
    public class Sorder
    {
        public virtual int Id { get; private set; }
        public virtual DateTime DateCreated { get; set; }

    }
}

Mapping:

namespace FluentNhibernationConsole.Mappings
{
    class SorderMap : ClassMap<Sorder>
    {
        public SorderMap()
        {
            Id(x => x.Id, "SorderId");
            Map(x => x.DateCreated);
        }
    }
}

Program itself:

namespace FluentNhibernationConsole
{
    class Program
    {
         private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration
                    .MsSql2005
                    .ShowSql()
                    .ConnectionString(@"server=.\sqlexpress;database=lsdb;Integrated Security=SSPI;")
                )
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>()
                    .ExportTo(@"d:\temp\nh")
                )
                .BuildSessionFactory();
        }

        static void Main(string[] args)
        {
            var sessionFactory = CreateSessionFactory();
            using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var order1 = new Sorder {DateCreated = DateTime.Now};
                    transaction.Commit();
                }
                using (session.BeginTransaction())
                    foreach (var order in session.CreateCriteria(typeof(Sorder)).List<Sorder>())
                        Console.WriteLine("Order: " + order.DateCreated.ToLongTimeString());
            }
            Console.ReadKey();
        }

    }
}

Answers


As you have already noted, you forgot to Save() your new entity.

using (var transaction = session.BeginTransaction())
{
    var order1 = new Sorder {DateCreated = DateTime.Now};
    session.Save( order1 );
    transaction.Commit();
}

And your ClassMap should be public

public class SorderMap : ClassMap<Sorder>

Need Your Help

How to give notification message to the currently logged in users using c#

c# sockets browser client-server

We are creating a new socket for the currently logged in user asynchronously.The server will notify this user at appropriate time then a pop up message should occur to the users who might be in dif...

Webpage login error-Android App

java android eclipse webpage login-script

I'm new to Android and Java. I'm creating an app to login to my ISP. This is the page: http://reliancebroadband.co.in/reliance/login.do