Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.
like image 345
mju516 Avatar asked Jul 19 '17 00:07

mju516


1 Answers

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

like image 124
niko2post Avatar answered Sep 23 '22 03:09

niko2post