简体   繁体   English

实体框架语言环境配置

[英]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.我遇到过这样的情况,查询将通过 EF 在应用程序中运行,但不在我的 SQL 控制台/IDE 中运行,反之亦然。 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.例如,当我将 VARCHAR2 列转换为 BINARY_FLOAT(如 linq 代码所示)时,即 50,23% 形式的数据被转换为 50,23,然后进行转换。 The sql console says it's anything with a comma is an invalid number, and EF doesn't. sql 控制台说任何带逗号的都是无效数字,而 EF 则不然。 Chaning that comma into a dot, will make EF report an ORA error "invalid number", but the console then works.将该逗号更改为一个点,将使 EF 报告 ORA 错误“无效数字”,但控制台随后可以工作。

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? EF 在哪里获取此本地化配置? More specifically how is it changing the decimal number format?更具体地说,它是如何改变十进制数字格式的?

This may be a CultureInfo issue, try setting the decimal separator explicitly?这可能是 CultureInfo 问题,请尝试明确设置小数点分隔符?

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.当您打开与数据库的连接并准备查询时,EF 将操作参数以避免 SQL 注入,就像使用添加参数函数一样。

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.当您在 SQL Studio 中明确编写错误的文化时,内联解析器将理解它不是正确的语言环境。

The same thing will happen in your code if you try to convert without the proper culture format.如果您尝试在没有正确文化格式的情况下进行转换,那么同样的事情也会发生在您的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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