how How can I generate NHibernate mapping files and DB constructs from my domain logic?

 

Questions


I want to implement NHibernate on my domain objects in my project, but I’m not sure how I should go about generating the mapping file, and the database.

I’ve found some questions that kind of touch on this here and here, but I’m starting with my classes already defined, and would like to start from them and work my way down, not the other way around.

Is there any way to do this?

I’m perfectly fine with a multi-stage process, I just want to know what other people have done that was successful for them.

FYI, I want to deploy the database on SQL Server 2005.

 

 

————————————————-

Answer

About the mapping:
You can create the mapping with the Fluent Mapping like Gary.
When you have a very uncomplicated domain model, you can use Automapping, a convention based mapping feature of FluentNhibernate:

 var sessionFactory = Fluently.Configure()  
   .Database(MsSqlConfiguration.MsSql2005  
     .ConnectionString(c => c  
       .Is(ApplicationConnectionString)))  
   .Mappings(m =>  
     m.AutoMappings.Add(AutoPersistenceModel.MapEntitiesFromAssemblyOf<Product>())  
   )  
   .BuildSessionFactory();

And that’s all you need.

You can build your database with schemaexport:

var schemaExport = new SchemaExport(configuration);
schemaExport.Create(false,true);

c#,database,nhibernate,nhibernate-mapping

Facebook Comments

Post a comment