Problem
A long-long time ago, I faced this problem while using NHibernate.
“Nhibernate threw an error when Reserved SQLserver keywords were used within mappings, queries, etc.”
I tried to find the solution for this issue and I did not find a good solution. The only one I found is just manually encode these reserved keywords. It was a real headache for me and I was having 1000s of things where I used these reserved keywords.
How To Resolve the Issue?
One day, I was working on some different project, where I need a lot of Extension methods and I got the idea. I found the solution for my problem and I found the following solutions. I did not remember where I found this code, but it is working.
public static class EncodeSqlReservered
{
public static string EncodeKeywordWithBraces(string keywordToEncode)
{
return string.Format("[{0}]", keywordToEncode);
}
public static string EncodeToBraces(this string keywordToEncode)
{
return string.Format("[{0}]", keywordToEncode);
}
}
End Notes
Here, I just shared the two extension methods, we can create as many as required. Also, we can use these methods directly in our mapping classes.
The post Nhibernate threw an error when used Reserved SQLserver kerywords with in mappings, queries, etc. appeared first on Gaurav-Arora.com.