简体   繁体   English

在构建Linq-to-Sql代码时强制实体框架不生成NCLOB(模型优先)

[英]Forcing Entity Framework to not generate NCLOB's when building Linq-to-Sql Code (Model First)

I have a class with a nullable int property that that for filtering reasons, I need to convert to a string to do some comparisons. 我有一个具有可空int属性的类,为了过滤原因,我需要转换为字符串来进行一些比较。 I have EF 6.1.2 installed, so using a .ToString() will work for that. 我安装了EF 6.1.2,因此使用.ToString()将适用于此。

queryableData = queryableData.Where(a => 
    a.PropName.HasValue && a.PropName.Value.ToString().Contains("8675309"));

When examining the actual SQL being executed, the number is being CAST into an NCLOB type, which is resulting in the following error: 在检查正在执行的实际SQL时,该数字正在被CAST转换为NCLOB类型,这会导致以下错误:

ORA-00932: inconsistent datatypes: expected NCHAR - got NCLOB ORA-00932:不一致的数据类型:预期的NCHAR - 得到NCLOB

From what I've read, this is because Entity is unaware of the potential max size in this case, so it defaults to the largest option. 从我读过的内容来看,这是因为实体在这种情况下不知道潜在的最大大小,所以它默认为最大的选项。 I know with a string property that I would be able to denote a max size to help. 我知道有一个字符串属性,我可以表示最大大小来帮助。 Is there anything that I can do while keeping the property as an int to prevent the NCLOB's from being used? 在将属性保持为int以防止NCLOB被使用时,我能做些什么吗? Or a way to use them while preventing this exception? 或者在防止此异常时使用它们的方法?

Some other notes: 其他一些说明:

  • I'm on an Oracle System, so SqlFunctions.StringConvert is out. 我在Oracle系统上,所以SqlFunctions.StringConvert出来了。
  • I'm on Odp.net version 12.x (related to this post ). 我在Odp.net版本12.x(与此帖相关)。
  • The EF is a Model-First approach. EF是一种模型优先方法。
  • The .Where() clause is being added to an AsQueryable(), so I can't do anything in memory. .Where()子句被添加到AsQueryable()中,因此我无法在内存中执行任何操作。

I have the same problem with Oracle ( Oracle 11.2.02 and Oracle.ManagedDataAccess.12.2.1100 ) and Entity Framework ( EntityFramework.6.1.3 ). 我在Oracle( Oracle 11.2.02和Oracle.ManagedDataAccess.12.2.1100 )和实体框架( EntityFramework.6.1.3 )中遇到了同样的问题。

This inside Linq code (the property "Id" is integer): 这内部Linq代码(属性“Id”是整数):

Material.Id.ToString()

Generates this SQL: 生成这个SQL:

(CASE WHEN ("Extent3"."Material_Id" IS NULL) THEN N'' ELSE TO_NCLOB("Extent3"."Material_Id") END)

And the problem is TO_NCLOB , it should be TO_NCHAR 问题是TO_NCLOB ,它应该是TO_NCHAR

Solution
JonathanPeel comment JonathanPeel发表评论
Install-Package EntityFramework.Functions Install-Package EntityFramework.Functions

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

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