c# - Entity Framework 6 - ORA-00932 while joining with ToString
问题描述
I'm testing out Entity Framework 6.2 with an oracle database but facing issues while trying to join on multiple conditions with convertion.
I need to match TABLE2_ID (NUMBER)
with TABLE2.ID (VARCHAR2)
which are different value types.
The problem is that the ToString()
method translates into TO_NCLOB
instead of TO_NCHAR
which would work.
var query = from table1 in context.TABLE1
join table2 in context.TABLE2 on table1.TABLE2_ID.ToString() equals table2.ID
select new
{
table1.NAME,
table2.TEXT
};
The generated SQL looks as following:
SELECT
1 AS "C1",
"Extent1"."NAME" AS "NAME",
"Extent2"."TEXT" AS "TEXT"
FROM "USER"."TABLE1" "Extent1"
INNER JOIN "USER"."TABLE2" "Extent2" ON
(CASE WHEN ("Extent1"."TABLE2_ID" IS NULL) THEN N''
// At this point I need TO_NCHAR
ELSE TO_NCLOB("Extent1"."TABLE2_ID") END) = "Extent2"."ID"
This results in:
ORA-00932: inconsistent datatypes: expected NCHAR got NCLOB
I know there are many questions with almost the same exception but only with different use cases, so please have a look how I can solve this problem.
For e.g. is there a way to override the ToString()
SQL translation?
MAIN PROBLEM: LINQ to SQL .ToString()
converts into TO_NCLOB
but I need TO_NCHAR
or another solution.
I use Visual Studio 2017 with Oracle Developer Tools for VS2017 12.2.0.11
解决方案
Sadly .ToString()
isn't implemented that well in Oracle Entity Framework. So I got a workaround by this SO question accepted answer.
Installing EntityFramework.Functions
Nuget Package and using the TO_NCHAR
built in Oracle function did the trick as following:
public static class OracleFunctions
{
[Function(FunctionType.BuiltInFunction, "TO_NCHAR")]
public static string ToNChar(this string value) => Function.CallNotSupported<string>();
}
Overriding OnModelCreating in DbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new FunctionConvention(typeof(OracleFunctions)));
}
And then I can use it like this which works flawless:
var query = from table1 in context.TABLE1
join table2 in context.TABLE2 on table1.TABLE2_ID.ToNChar() equals table2.ID
select new
{
table1.NAME,
table2.TEXT
};
推荐阅读
- mysql - 带有 mysql 的 Spring Boot 应用程序卡在“Hikari-Pool-1 - 正在启动...”
- elasticsearch - Elasticsearch:自定义分析器中 synonym_graph 和停止过滤器之间的意外交互
- ssl - 如何使用 scapy 解密从 pcap 文件读取的 TLS 流量?
- salesforce-marketing-cloud - 我正在尝试过滤所有订阅者中的订阅者,以识别通过创建配置文件属性从哪个 MID 创建订阅者
- java - Java sftpChannel put error (系统找不到指定的路径)
- iframe - 腾讯(视频)在 iframe 中做了什么讨厌的事情?
- java - 当属性文件没有值时,我有不想创建的 bean 列表,如何实现?
- javascript - Chartjs 折线图 - 数据的数组数组
- c - 使用结构的年度学生计划
- c# - 将 FormFile 转换为流