Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Identity columns with NHibernate - to use or not to use

I'm working with SQL Server, and most of tables have identity columns of type int. And now, I just came across a book, that tells it's not a good idea to use this mechanism. What are the cons/pros of using identity columns when using NHibernate as ORM?

like image 723
dragonfly Avatar asked May 29 '12 20:05

dragonfly


2 Answers

I'm not an NHibernate expert, but as far as I understand it, using identity columns breaks the UnitOfWork and causes additional round-trips to the database.

Fabio Maulo (then project lead of NHibernate, I'm not sure if he still is today) has a blog post where he explains this in detail:
Identity: The never ending story

Here is another (longer) blog post with example code:
NH2.1.0: generators behavior explained

However, note that both blog posts are about three years old and talk about NH 2.1.0, and the current version as of today is NH 3.3. But I assume that the problem as described by Fabio still exists today.

like image 189
Christian Specht Avatar answered Oct 18 '22 08:10

Christian Specht


It makes no difference if they are an identitity column or not. The role of an ORM is to map the data entities to the database not to dictate the database structure/design. I have used an implementation with identity columns and it works fine.

like image 28
user1378687 Avatar answered Oct 18 '22 08:10

user1378687