Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

NHibernate Threw An Error Reserved SQLserver Keywords Were Used Within Mappings, Queries, etc.

0.00/5 (No votes)
13 Aug 2015 1  
NHibernate threw an error when used Reserved SQLserver kerywords with in mappings, queries, etc.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here