我用Google搜索了但是我找不到任何有关如何通过代码配置nhibernate 3.3的实际示例。这是我可以找到但它不起作用,它抛出异常“用户必须提供ADO.NET连接”
var cfg = new Configuration();
cfg.DataBaseIntegration(c=> c.Dialect<MsSql2008Dialect>());
cfg.SetProperty("hibernate.connection.connection_string", "Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;")
.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver")
.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
var mapper = new ConventionModelMapper();
mapper.Class<User>(map =>
{
map.Id(x => x.Id, m => m.Generator(Generators.Guid));
map.Property(x => x.UserName);
});
var mapping = mapper.CompileMappingFor(new Type[] { typeof(User) });
cfg.AddDeserializedMapping(mapping, "test");
var factory = cfg.BuildSessionFactory();
var session = factory.OpenSession();
session.SaveOrUpdate(new User() { Id = Guid.NewGuid(), UserName = "Hello" });
session.Flush();
session.Close();
factory.Close();
有什么问题?缺什么?在哪里可以找到一个有效的例子?
由于
答案 0 :(得分:1)
新的流畅NHibernate配置称为Loquacious。您可以在an introduction to it博客上找到James Kovacs。
另外,请查看this SO question。它有一组链接,主要用于按代码功能进行映射:
Getting started with NHibernate 3.2 Loquacious API
至于您的配置,您不需要使用.SetProperty()调用来初始化数据库连接。相反,使用这样的东西:
var cfg = new Configuration();
cfg.DataBaseIntegration(c=>
{
c.Dialect<MsSql2008Dialect>());
c.ConnectionString =
"Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;";
}