views:

23

answers:

1

Dear All!

This may be a really silly question, but I've found no answer on google/bing...

If I already use NHibernate for persistence with SQL Server, why should I then create all the table-relations on the database-schema? I'm just wondering because it seems to create all the relations, altough I already have defined them in the NHibernate-mapping.

Best regards, Daniel Lang

A: 

If the schema was generated via NHibernate, it already established all the relations on a schema level. Now, if NHIbernate was not used in the generation of the schema, you would still be advised to establish them, because there's always the chance that a coding mistake could lead to database operations which would break the relationship rules, but NHibernate is ignoring due to a bug in the code.

An example of this would be a relationship with a Not Null constraint. If NHibernate is configured incorrectly, it may very well try to update an object and set the relation to Null (say, instead of deleting it), which would be really hard for you to debug without the correct relations and/or an NHibernate debugging tool like NHProf.

Rafael Belliard
Ok, I understand that the risk of inconsistency due to bugs could be a reason for implementing relations on the db-schema - but has it anything to do with sql-server performance when it comes to table joins?
dlang
Well, database performance can be improved with adding indexes. But foreingn key are not indexes thus you won't improve performace with them.
Petr Kozelek