我正在尝试一个非常简单的 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();
}
}
}