Basic Fluent NHibernate 程序编译并运行,但没有任何内容保存到数据库或从数据库加载

发布于 2024-08-23 07:29:44 字数 2083 浏览 7 评论 0原文

我正在尝试一个非常简单的 Fluent Nhibernate 示例: SQL 2005 数据库,一张表,VS2008 控制台应用程序。在程序启动之前该表有一条记录。

我正在尝试添加一条记录,然后显示表中的所有记录。程序编译成功,运行没有任何异常,但没有显示任何记录。也不会创建 HBM 映射文件。看起来该程序完全忽略了数据库(尽管它连接到了数据库)。

这是我的代码 - 我试图使其保持最小:

实体:

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

    }
}

映射:

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

程序本身:

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

    }
}

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2024-08-30 07:29:44

正如您已经注意到的,您忘记了 Save() 您的新实体。

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

你的 ClassMap 应该是公开的

public class SorderMap : ClassMap<Sorder>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文