2

I googled it but I couldn't find any working example how to configure nhibernate 3.3 by code. Here is what I could find but it doesn't work, it throws the exception "The user must provide an ADO.NET connection"

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

What is wrong? What is missing? Where I can find a working example?

Thanks

Spamme
  • 21
  • 1
  • 2

1 Answers1

1

The new fluent NHibernate configuration is called Loquacious. You can find an introduction to it on James Kovacs blog.

Also, take a look at this SO question. It has a collection of links, mostly for mapping by code functionality:

Getting started with NHibernate 3.2 Loquacious API

As for your configuration, you don't need to use .SetProperty() calls to initialize database connection. Instead, use something like this:

var cfg = new Configuration();
cfg.DataBaseIntegration(c=> 
    {
       c.Dialect<MsSql2008Dialect>());
       c.ConnectionString = 
           "Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;";
    }
Community
  • 1
  • 1
Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47