2

I am working on an Application utilizing Entity Framework for Data Access. For some purposes it is neccessary to parse the SqlException Message for further information. I already found out that this Exception Message is directly coming from SQL Server. So to make that parsing process work i need to make sure the language is set to the same in every environment.

I am using to set the language

SET LANGUAGE English;

For test purposes I did following:

SET LANGUAGE English;
SELECT * FROM ABC;

ABC does not exist so it will obviously fail and i will get an error message. In Management Studio my ErrorMessage is showing this:

enter image description here

this is in English as supposed to.

Now when i try the same in C# i will get:

enter image description here

which is in german.

What am i missing? Any ideas anyone? Thanks very much...

4
  • 2
    I don't know much about EF but if it were to open & close the connection within the ExecuteSqlCommand method that would certainly explain it as SET LANGUAGE has session scope. Commented May 29, 2019 at 15:42
  • 1
    Pretty sure ExecuteSqlCommand will start a new auto commit session. So your first SET LANGUAGE pretty much has no effect. Commented May 29, 2019 at 15:53
  • I think the "SET LANGUAGE" command is only used by the database engine for comparison rules etc. You need something at the EF level to set the local error message. Perhaps "CultureInfo.CurrentCulture = new CultureInfo("en-US");" Commented May 29, 2019 at 15:55
  • Changing the CurrentCulture wont work as the message is directly coming from SQLServer. But yes executing it in one command works. Commented May 29, 2019 at 15:57

1 Answer 1

3

instead of doing this in two ExecuteSqlCommand calls you should do this in one.

string command = "SET LANGUAGE English; SELECT * FROM ABC;";
ctx.Database.ExecuteSqlCommand(command);

ExecuteSqlCommand will start a new auto commit session. Your first SET LANGUAGE pretty much has no effect since the select is in another session.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes that is correct Thank you very much. Now i will need to find a way to change the language when querying the database with entity framework directly. So injecting the command before EF will execute the query.
Ok so with this learn.microsoft.com/en-us/sql/database-engine/configure-windows/… I can set it permanently and that solved the issue. Thanks very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.