问题描述
我有asp.net mvc 4应用程序与EF 4,.net 4此代码
public List< ;列表项> GetViolatedArticlesByLaw(int lawId,string culture)
{
culture = culture.ToLower();
var ans =
(从上下文中的艺术.TITLENORMALAWs.Where(l => l.PARENTID == lawId)
选择新的ListItem
{
ID = art.ID,
ParentID = lawId,
Value =(art.NUM_STATSTR ??),
});
Debug.WriteLine(((System.Data.Objects.ObjectQuery)ans).ToTraceString());
return ans.ToList();
}
throws ora-12704:字符集不匹配。
如果我从列表中选择,它运行得很好,如下所示: from context.TITLENORMALAWs.Where(l => l.PARENTID == lawId).ToList()
这是生成的SQL:
SELECT
Extent1。IDASID,
:p__linq__1 ASC1,
CASE WHEN(Extent1。NUM_STATSTRIS NULL)THEN''ELSEExtent1 NUM_STATSTREND ASC2
FROMAISSMKTITLENORMALAWExtent1
WHERE(Extent1。PARENTID=:p__linq__0)
它在sqldeveloper中产生相同的错误,如果我更改这个 THEN'ELSE
到这个 THEN n''ELSE
它运行正常。
表定义中的NUM_STATSTR是NVARCHAR2(30)
如何使linq生成正确的sql?或者我必须在选择之前调用 ToList()
?没有其他方法?
@Orif我想你应该尝试手动构建查询,而不是使用LINQ to SQL生成器。
尝试使用 ExecuteQuery
方法在 DataContext
类,并尝试添加一个转换为 NVARCHAR
有关更多帮助,请阅读这里,
I have asp.net mvc 4 application with EF 4, .net 4This code
public List<ListItem> GetViolatedArticlesByLaw(int lawId, string culture)
{
culture = culture.ToLower();
var ans =
(from art in context.TITLENORMALAWs.Where(l => l.PARENTID == lawId)
select new ListItem
{
ID = art.ID,
ParentID = lawId,
Value = (art.NUM_STATSTR ?? ""),
});
Debug.WriteLine( ((System.Data.Objects.ObjectQuery)ans).ToTraceString() );
return ans.ToList();
}
throws ora-12704: character set mismatch.It runs perfectly fine if I select from List, like this: from art in context.TITLENORMALAWs.Where(l => l.PARENTID == lawId).ToList()
This is the SQL generated:
SELECT
"Extent1"."ID" AS "ID",
:p__linq__1 AS "C1",
CASE WHEN ("Extent1"."NUM_STATSTR" IS NULL) THEN '' ELSE "Extent1"."NUM_STATSTR" END AS "C2"
FROM "AISSMK"."TITLENORMALAW" "Extent1"
WHERE ("Extent1"."PARENTID" = :p__linq__0)
It produces the same error in sqldeveloper and if I change this piece THEN '' ELSE
to this THEN n'' ELSE
it runs ok.NUM_STATSTR in table definition is NVARCHAR2(30)
How can I make linq generate proper sql? Or do I have to call ToList()
before selecting and there is no other way?
@Orif I think you should try to build the query manually instead of using the LINQ-to-SQL generators.
Try using the ExecuteQuery
method on the DataContext
class and try to add a cast to NVARCHAR
For more help read here, https://social.msdn.microsoft.com/Forums/en-US/20d456f0-9174-4745-bbc5-571f68879e27/net-strings-sql-paramater-type-always-nvarchar?forum=linqtosql
这篇关于C# ??操作符和ora-12704:字符集不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!