Generate C# entities from existing DB and Fluent NHibernate auto mapping - c#

I'm working with an existing database that uses some really ugly conventions. I'd like to use NHibernate, and I think I can fix all these ugly DB conventions using Fluent NHibernate's auto mapping conventions. I'd like to avoid writing all the entity classes by hand. (This is where LINQ to SQL and SubSonic are appealing)
Is it possible to generate my C# entities based on my database schema and mappings specified in Fluent NHibernate?
Aside: Do you know of a designer for NHibernate?

I think, this is what you want: NHibernate Mapping Generator.
http://nmg.codeplex.com/

NHibernate Mapping Generator supports Fluent NHibernate and a variety of databases.
There are also plans to support the new NH 3.2 style fluent mappings.
Have a look.

LLBLGenPro V3 will work as a generic designer for a number of ORM frameworks, including LLBLGen's own framework, NHibernate,Linq2SQL and the Entity Framework.
It also includes template for generating code and mappings for all of the above frameworks.
Fluent NHibernate is under consideration at the moment :-)

I have not found anything that will generate Fluent NHibernate code based on an existing database schema.
Here's a similar question:
Using MyGeneration with Fluent NHibernate
As far as designer for NHibernate, there is ActiveWriter, but that is for Castle ActiveRecord, not for Fluent NHibernate. I remember seeing a reference to at least one other tool that could act as an NHibernate designer, but I can't find it at the moment.

Code warrior provides a bunch of templates for creating entities from database tables. I used it to generate my entities & services & unit tests, then used fluentNH automapping. This got my project up and running very quickly. the project needed a bit re-factoring but what code base doesn't.

Related

Using Solrnet and Assigning Attributes with Entity Framework Generated POCOs

I will be using Solr search server with my ASP.NET 4.5 application. I've already installed SOLR on my Windows 8 laptop computer. According to SolrNet this documentation, I need to use specific attributes on my POCOs.
The thing is that I am using Entity Framework and my classes are auto generated. Is there an option to assign those type of Solr attributed and also make sure that they are presistant and won't be erased if your suggested solution is based on editing the template (.tt) file.
I want to use Entity Framework, but if it is not possible, I will just copy the pocos and create the classes myself with those Attributes. But I prefer searching for a solution that will allow me to use solrnet with Entity Framework. Thanks.
I would suggest that you create separate classes that map to your Solr Index schema, as typically the structure of your EF classes and your index schema will not be identical. This way you have a clean separation between your persistence classes (those auto-generated by EF) and your index mapping classes and can control how the mapping between the two occurs. I recommend the use of AutoMapper to assist with translating your objects from EF to Solr and back again as needed.

Nhibernate mapping generator that supports mapping by code from 3.2

I am looking for an NHiberate mapping generator that can generate mapping by code rather than .xml or Fluent NHibernate.
I tried NHibernate Mapping Generator, but it has no validation. Thus, the existing mapping files might not 100% correct.
Devart Entity developer doesn't support this currently.
Any recommendations?
You could try nHibernate Designer from MindScape.
Well since you are asking for a suggestion, I can say by experience don't try to use a code generator. Such kind of approach fails when the project grow, you suddenly need to add some modifications and re-generating the entities would be a pain and so on. Try to read about ConventionalMapping. It is basically a strategy in building the mapping on the fly based on some conventions generally exists in a database table vs classes naming. I did this in past and with a little effort you will be able to concentrate just on the classes and completely forgot the mapping.

CodeFirst C# Generation

I would like to use a tool to generate C# CodeFirst Entities and Mappings. I have used Entity Framework Power Tools and am looking for a better solution.
Using Entity Framework Power Tools as an example. This allows all tables in a database to be reverse engineered with C# classes generated for each Entity and Mapping. The problem is that you have to generate them all and cannot select just a few.
Is there such a tool that can generate these C# Entities and Mappings for the tables that you select? I do not want to hand-code these C# Code First classes.
If you have an existing database you probably could use model first approach with DbContext templates. Take a look at this walkthrough http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-model-amp-database-first-walkthrough.aspx. You can modify your model after creating it from the database. Also, when doing this this blog post may be useful too: http://blog.oneunicorn.com/2012/02/26/dont-use-code-first-by-mistake/

Fluent NHibernate or NHibernate for complicated database with complicated relations

I have a databse with complicated relations. I need to know if is better for my project use Fluent NHIbernet or only NHibernate. I know than fluent is better for auto mapping etc. . But will i have some problems in future when I have complicated databes with realy complicated relaations. Have Fluent NHibernate some limitations. Have Fluent NHibernate some problems during configuration? Will be Fluent NHibernate slower as NHibernate?
someone correct me if im wrong, but I think under the hood fluent nhibernate creates the xml mapping files that get loaded into nhibernate.
You can configure fluent-nhibernate to handle both the ClassMap<> and the xml based .hbm.xml mapping files. If there are situations you run into that fluent-nhibernate cannot handle then you can drop back to the xml mapping files if needed. The advantage of the fluent-nhibernate mapping is that it is easier to refactor your POCO classes as you have compile time checking, where as the xml mappings you will only know if there is a problem at runtime.
The question is not constructive. The only difference between NH and FluentNH is XML binding definitions vs. in-code binding definitions. FluentNH only defines the schema, it does nothing to the ORM logic itself - it's still NHibernate.

How to convert Castle Activerecord to pure NHibernate or Fluent NHibernate?

I'm going to refactor a growing project from using Castle Activerecord to pure NHibernate or Fluent NHibernate with Service/Repository pattern and POCO.
What would be an easiest way to obtain hbm xml from existing Castle Activerecord model?
Another question, is it possible to convert hbm to Fluent NH and vice versa?
Remi already pointed out how to generate hbm from ActiveRecord. It's also possible to generate hbm mappings from Fluent NHibernate by using ExportTo(). It's not possible to convert hbm to FNH (that is, C#) code, but you can load them from FNH.
Using Castle ActiveRecord does not imply that you can't use repositories. In fact, Castle ActiveRecord does not force you to use the ActiveRecord pattern, and it doesn't even require you do inherit ActiveRecordBase in your persistent classes. You can use ActiveRecordMediator as a repository, or wrap it in a repository interface/implementation as Rhino.Commons does.
http://stw.castleproject.org/Active%20Record.Configuration%20Reference.ashx
You can also output nhibernate mapping
files to the AppDomain.BaseDirectory.
<activerecord isDebug="true"><config></config></activerecord>
I think that should give you the mapping file in your application directory
There is at least one open source project to convert HMB to FNH:
http://code.google.com/p/nhibernate-hbm-to-fluent-converter/
As others have already said, you can easily convert ActiveRecord to HBM and FluentNHibernate to HBM.

Categories