Saturday, August 20, 2011

use Entity Framework and NHibernate together if you dare!!!

In our app we have a best of both worlds thing going on in which we embrace BOTH Entity Framework and NHibernate. By best of both worlds, I mean that we mostly use NHibernate, but we have wired up the Entity Framework GUI tools to work with NHibernate. A T4 Template generates NHibernate wire ups when an .edmx file is saved. The .edmx file shows a snapshot of how all of our entities interface. Very cool stuff.


 
 

I have not used Entity Framework in a very long time (because no one else does) and I tried to familiarize myself with it anew some today (you may see as much in prior blog postings), and I wrote some notes like this...

  • Right-click in an .edmx's GUI layout and pick "Generate Database from Model..." from the menu that appears to shove a model to a database (you will have to pass through an intermediary step as a SQL script will appear in Visual Studio and you will need to right-click in the SQL script and pick "Execute SQL")

...but I ultimately gave up on trying to spin it up in a new app to really understand it when I started hitting enough obstacles to discourage me. I don't need to know Entity Framework for the project I am on, I just need to understand .edmx files. In them, one may click on associations (to make seletions as shown below) and then right-click to delete an association.


 
 

One may right-click in an .edmx and pick "Add Association..." to craft Associations. By this process, I have, in this example, changed a many-to-many association to a one-to-many association. Notice the change in column name that resulted.


 
 

Employer and Graduate aren't real entities in our app. ;)

6 comments:

  1. I'm moving from NHibernate to Entity Framework, for the next project or two anyway, and had this same idea while learning EF. I previously despised code generation, but I am impressed with the "model" idea. I like it so much, in fact, I think it should be ORM-agnostic. Can you share the T4 template you mentioned above? Do you know of other resources around the web about this?

    ReplyDelete
  2. We talked about open sourcing the T4 template. Let me check with my higher-ups and see their thoughts about sharing it.

    ReplyDelete
  3. That would be great. In the meantime, could you answer or comment on my question over here: stackoverflow.com/questions/9300034/

    ReplyDelete
  4. A higher-up told me I cannot officially help you. And yet... check your mail...

    ReplyDelete
  5. Pretty please, can I peek at this T4 too? i-um@ya.ru

    ReplyDelete
  6. I haven't had my hands in this in over a year.

    ReplyDelete