Click here to Skip to main content
15,559,921 members
Articles / Programming Languages / C# 5.0
Technical Blog
Posted 13 Aug 2015

Stats

4.5K views
2 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
13 Aug 2015CPOL
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
There is no age limit for learning and its never late, to start.

Comments and Discussions

 
QuestionExample code Pin
Shuby Arora14-Aug-15 10:17
Shuby Arora14-Aug-15 10:17 
AnswerRe: Example code Pin
Gaurav Aroraa16-Aug-15 5:19
professionalGaurav Aroraa16-Aug-15 5:19 
Sure, I will attach source code, till then please get this from github.com/garora

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.