Nhibernate threw an error when used Reserved SQLserver kerywords with in mappings, queries etc

Problem

A long-long ago, I faced this problem while using NHibernate

“Nhibernate threw an error when used Reserved SQLserver kerywords with in 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 really head-ache for me and I was having around 1000s things where I used these reserved keywords.

How to resolve the issue?

One day, I was working on some different project, where I need lot of Extension methods and I got the idea. I found the solution for my problem and I found following solutions did not remember, where I find 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 many as required. Also, we can use these methods directly in our mapping classes.

Leave A Reply