Basic Fluent NHibernate 程序编译并运行,但没有任何内容保存到数据库或从数据库加载
我正在尝试一个非常简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您已经注意到的,您忘记了 Save() 您的新实体。
你的 ClassMap 应该是公开的
As you have already noted, you forgot to Save() your new entity.
And your ClassMap should be public