The Fluent NHibernate core program compiles and runs, but nothing is saved or loaded from the database

I tried a very simple Fluent Nhibernate example: SQL 2005 single-table database, VS2008 console application. Before starting the program, the table has one entry.

I am trying to add one record and then display all the records from the table. Programs are successfully compiled and run without any exceptions, but entries are not displayed. An HBM mapping file is also not created. It seems that the program completely 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);
        }
    }
}

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

    }
}
+3
1

, () .

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

ClassMap .

public class SorderMap : ClassMap<Sorder>
+2

Source: https://habr.com/ru/post/1734634/


All Articles