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

南楼画角 提交于 2021-02-19 00:34:36

问题


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. I have EF 6.1.2 installed, so using a .ToString() will work for that.

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:

ORA-00932: inconsistent datatypes: expected NCHAR - got 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? Or a way to use them while preventing this exception?

Some other notes:

  • I'm on an Oracle System, so SqlFunctions.StringConvert is out.
  • I'm on Odp.net version 12.x (related to this post).
  • The EF is a Model-First approach.
  • The .Where() clause is being added to an AsQueryable(), so I can't do anything in memory.

回答1:


I have the same problem with Oracle (Oracle 11.2.02 and Oracle.ManagedDataAccess.12.2.1100) and Entity Framework (EntityFramework.6.1.3).

This inside Linq code (the property "Id" is integer):

Material.Id.ToString()

Generates this 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

Solution
JonathanPeel comment
Install-Package EntityFramework.Functions



来源:https://stackoverflow.com/questions/45179112/forcing-entity-framework-to-not-generate-nclobs-when-building-linq-to-sql-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!