Click here to Skip to main content
15,918,333 members
Articles / Programming Languages / C# 5.0

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 5.1K   2   2
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
Chief Technology Officer
India India
Learning never ends.

Comments and Discussions

 
QuestionExample code Pin
Shuby Arora14-Aug-15 9:17
Shuby Arora14-Aug-15 9:17 
can you please provide example coede?

AnswerRe: Example code Pin
Gaurav Aroraa16-Aug-15 4:19
professionalGaurav Aroraa16-Aug-15 4:19 

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.