In Entity Framework it is possible to combine fluent configuration with attribute-based configuration.
As in NHibernate both configuration approaches are different projects I guess they're not combineable, however, maybe I'm misunderstood.
Is it possible to combine both approaches in NHibernate or not?
No, you have to choose what type of configuration (or mapping) you want to do. Fluent NHibernate, Xml, Attributies. You do not need to create your configuration in a separated project, but it is a good pratice to keep the single responsability for each part of your code. I really recommend you mapping using the fluent NHibernate.
This article show each type of mapping:
http://www.davesquared.net/2008/09/nhibernate-mappings-using-xml.html
Related
I have several projects using NH and FNH to generate the mappings (some Fluent some Automapped). There are still some bugs and missing features i need, but it seems that FNH could die because of mapping-by-code integrated into NHibernate.
Question: Contribute to FNH or to migrate the mappings to mapping-by-code or confORM and fixing problems/implementing features there?
At our office, we have been using NHibernate for 3 years now. We've been thinking about making a move to Fluent Nhibernate but never made the move. Using hbm.xml files was still the easiest to debug/alter. Two common problems of those xml files are that they are all validated during the creating of the sessionfactory and they are not refactor-safe.
Due to a bug I had to update a newer release of NHibernate (we were using NHib 2.1.2GA) and when I implemented 3.2GA we also were handed the ability to use loquacious mappings (mapping by code). I decided to use Loquacious over Fluent because I don't have a dependency to another project (Fluent) and the fact that NHibernate won't be shipped if mapping by code is broken.
Be aware though, that Loquacious mapping isn't complete either. While I was mapping everything by code, I found out basic stuff like property-ref wasn't always implemented. Thus even though it's shipped, it's not 100% complete. And while this will not come as a shocker, it has bugs. yes. really. ;-)
for more info on (reported) bugs, check out the NHibernate bug database: https://nhibernate.jira.com/browse/NH
Hope this helps.
Regards, Ted
thx to #TedOnTheNet i will continue to use and contribute to FNH, because it will take a while until mapping-by-code reaches FNH in some areas
Automapping
Fluent API tells a lot better which possibilities there are
far more verbose mappings
.Database(SQLiteConfiguration.Standard.InMemory()) is still easier to figure out than
.DataBaseIntegration(db =>
{
db.ConnectionString = ???;
db.Dialect<SQLiteDialect>();
db.Driver<???>();
});
and some features:
CompositeId and Table per Subclass
default values for legacy columns
property refs
Update: some features from hbm.xml (and FluentMapping) will not be possible at all with mapping by code:
default values for legacy columns: database column to constant value without the need for a property in the entity class with mapping by code
query only properties/collections: http://ayende.com/blog/4054/nhibernate-query-only-properties
NH 3.2 does not have anything remotely equivalent to FNH's Automapping, as far as I can tell. (For me, that would be a dealbreaker).
Edit
The FNH Automapper can deal with most of the common patterns in an object model, such as inheritance, one-to-many relationships, self-referencing, etc - without requiring any help from the programmer. So far, pure NH has not achieved this level of automation.
Also, James Gregory has publicly stated that he will continue to develop FNH, at least in the near term. (Think I saw this on the FNH Google group a few months back, but I'm not sure exactly where).
I want to use Fluent NHibernate with NHibernate. But I want to find out whether Fluent NHibernate support all mappings from NHbernate. Or is it better to stick with NHibernate?
Fluent NHibernate supports automappings, standard nhibernate xml mappings (hbm) and of course Fluent style mappings. You can mix and match any of those mappings with Fluent NHibernate.
Fluent does not support the new mapping by code style introduced in version 3.2 (they're really different animals) of NHibernate but I'm not sure that this is really an issue. Personally I would recommend using Fluent at this point because there is far more documentation and the user base is larger than mapping by code.
Fluent NHibernate is fairly easy to use and definitely has it's advantages over xml style (hbm) mappings. You can read about this more on the Fluent Wiki
just FYI; I tried using standard hbm.xml for mapping "many-to-any" which is currently not supported by fluent nhibernate;
tried both Configuration.AddXmlFile and HbmMappings
both still failed with error message saying invalid properties "many to any"
I am trying to integrate NHibernate into an existing application with several hundred tables. Due to the fact that there apparently wasn't a strict adherence to conventions, I am unable to use Automap. As a result, I'm going to use Fluent to manually map over all of the associations.
Rather than doing it by hand, I'm hoping that there is a tool in existance that can either run through my solution or the database and stub out mappings.
Does anyone know of any project out there with this in mind?
Thanks!
I don't know of a tool that does it in Fluent, but there are plenty of options for doing it with the old hbm xml files. CodeSmith and MyGeneration are two that are recommended as a starting point for your classes and hbms.
LLBLGen Pro V3 will do exactly this, it can generate either tradition .hbm.xml or fluent mappings for your entire database.
Old question, maybe still relevant to some. If you are looking for an open source solution, try NHibernate Mapping Generator
I'm looking for a NHibernate Mapping generator which could generate a mapping file (hbm) and SQL Script from a C# class. I've found generators for the other way around, but not for this case.
Can you guys point me to nice generator?
Thanks a lot!
With Fluent NHiberate you can do without mapping files. In the instances where the automapping doesn't work, you can override.
This post discusses schema generation
Fluent Nhibernate Schema Generation
The newest, fastest and most extensible way to configure NHibernate is ConfORM.
You don't need to generate xml files (although you can do so if you want). It's an order of magnitude faster than Fluent NHibernate and, overall, less work.
For some examples, read Fabio Maulo's posts.
I know that you can generate a database from classes and their mappings using nHibernate, but I can't find any good links to do this the other way around. We have a database that has already been designed, and we are looking at using nHibernate. It would be nice to use a tool to generate the mappings and classes from the database, and then we could tweak/manipulate them to suit our tastes.
NHibernate Mapping Generator
You can use nhibernate with an existing database. It is a matter of writing the mapping files.
I also recommend using Fluent Nhibernate...I started using this after this community recommended it..
Look at subsonic as well if you do not like maintenance of mappings.