map composite keys in Nhibernate

I have a legacy database where 2 columns are used in all the table create composite keys .

One of he error message which you may see :

must have same number of columns as the referenced primary key nhibernate

Solution :

Table1

ProjectID Version Project Name

Table 2

ProjectID Version Table2ID Name1

Table 3

ProjectID Version Table2ID Table3ID Name2

I have a common Composite key Project ID and Version from Table 1.




Mapping will look like this 



that’s it Smile


 


		
			

Call Stored Procedure from Nhibernate

This may not the most efficient way to call stored procedure form Nhibernate using projects. But in my situation it first .In general when we uses  Nhibrnate there is almost no need to use. In my case I am mapping my application with Legacy Database so i need sometimes to call Stored Procedures from Nhibernate (though iI am trying my best to avoid this )..

So here is the simple way to use it pretty quickly :

1. Here is my Stored procedure

2. Here is the code snippet

Nhibernate error :Incorrect syntax near the keyword ‘Function’.

In our database sometimes we have column names which are actually the database keywords . If we want to access such columns from Nhibernate session  , it throws error message “Incorrect syntax near the word :’SOME DB KEYWORD’

 

Mapping :Before :

Mapping :After

Keeping database base reserve keyword in “[ ]”  brackets will fix the error message.

 

You can have look on all my previous Nhibernate posts

 

 

 

 

 

 

 

 

 

 

 

User Nhibernate with Legacy database

Topday we will talk little bit more about the Nhibernate topic. If you are new to this topic than

you can have look at  previous post

As its well accepted that Nhibernate is getting really (probably) first choice in applcation development
Its really easy to stary with Nhibernate for a new application development. You have more control
on your application  code and its db realtions .
Well  this doesnt go as smooth with Legacy DB as new application.

I have legacy database and it has many relations which could drive tyou crazy like anything .

Here in this post I will present a quick but very useful ways to map you WIERED realtions in Nhibernate using fluent Nhibernate.

Here is my small datamodel to explain this kinda of mapping

Data model

Mapping

 

Above mapping will create a mapping for the ToolLangTbl.

IF you notice int he mapping we have used CompositId to map such mappings.

Note :

In such cases always implement GetHashCode and Equal methods as below

 

 

If you still have some other wiered database relations and need help to map them ..I can try to help you in it.

Have a nice day ahead !!

 

 

“No Persister For” error in Nhibernate

Hello Friends

I was running into some other problem and I wanted to test some thins so I have quickly created a small solution.I did all the required steps and run it ..and voilaaa…

i have this small cute error (because its a really small text 🙂 ) It says “No Persister for :….

I get this error when try to save object or SaveUpdate any object.  After a while I figured out that forgot to make my Mapping class Public.

And it resolve the problem.

My DataModel Class

namespace NHBData.Model

{

public class Employee

{

public virtual int EmployeeID { get; set; }

public virtual string  EmaployeeName { get; set; }

}

}

 

Mapping file before :

namespace NHBData.Model.Mapping

{

class EmployeeMapping :ClassMap

{

EmployeeMapping()

{            Id(x => x.EmployeeID).Unique();

Map(x => x.EmaployeeName);

}

}

}

Mapping File after

namespace NHBData.Model.Mapping

{

Public class EmployeeMapping :ClassMap

{

EmployeeMapping()

{            Id(x => x.EmployeeID).Unique();

Map(x => x.EmaployeeName);

}

}

}


You can have a look to another annoying error and its solution “LazyLoadException” in nhibernate here

 

Hope this helps to someone..

Have a nice time ..

 

 

 

Nhibernate Self Reference uni-directional

In my previous post we have seen to implement self reference (Bi-directional) . Today we will quickly see  how to achieve self reference uni-directional.

Scenario

Lets take a simple scenario of a tree structure . One node can have many Children nodes but any child node will have only one parent.

Node.cs

NodeMap.cs

Now  we do not have any mapping table because we want it unidirectional mapping. In database we have only one table Node . Lets see how to get  complete tree now ..

Above mapping will create the below realtion

You can refer below link for more details :

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/05/14/how-to-map-a-tree-in-nhibernate.aspx

You can download complete implementation from here