简体   繁体   中英

Entity framework locale configuration

I have encountered a situation where the query will run in the app, via the EF, but not in my SQL console/IDE, and vice versa. For example, when I cast the VARCHAR2 column into BINARY_FLOAT (as shown in the linq code), ie data of the form 50,23% is converted to 50,23 and then casted. The sql console says it's anything with a comma is an invalid number, and EF doesn't. Chaning that comma into a dot, will make EF report an ORA error "invalid number", but the console then works.

var query = db.OWNER
                .Where(o => o.CASE_ID == caseId && o.STOCK != null && o.STOCK.EndsWith("%"))
                .Select(o => o.STOCK.Replace("%", "")).Cast<float>();
var result = query.ToList();

Where is EF picking up this localization configuration? More specifically how is it changing the decimal number format?

This may be a CultureInfo issue, try setting the decimal separator explicitly?

CultureInfo culture = new CultureInfo("en-US");
culture.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

There is a difference in culture between your current thread and the database.

When you open the connection to the database and preparing the query, EF will manipulate the parameters to avoid a SQL injection as it does with the add parameter functions.

At this point, whether it is a date object or a decimal or any other culture specific data type, the final string that will be the query, is transformed to the database culture specific format.

When you explicitly write the wrong culture in the SQL studio, the inline parser will understand that it's not the correct locale.

The same thing will happen in your code if you try to convert without the proper culture format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM