linq - 使用 odp.net 的 Oracle Entity Framework 不采用 linq 查询中的参数

标签 linq entity-framework odp.net

我正在使用 odp.net 将 EntityFramework 与 Oracle 结合使用。参数化的 sql 查询不起作用。

var orderCode = "XYZ";
var set = ctx.Database.SqlQuery<Order>(
    "Select * from dwh.Orders where OrderCode = '{0}'"
    , orderCode
);

(或者)
var set1 = ctx.Database.SqlQuery<Order>(
    "Select * from dwh.Orders where OrderCode = ':param'", 
    new OracleParameter("param", orderCode)
);

Console.WriteLine(set.Count() + ", " + set1.Count()); //Gives 0, 0

但是,如果我对该值进行了硬编码,它就可以工作。
var set = ctx.Database.SqlQuery<Order>(
    "Select * from dwh.Orders where OrderCode = 'XYZ'",
    orderCode
);

有谁知道为什么?我在该 View 中有 150 列。那是问题吗?

更新:
带有 Oracle 参数的查询有效。问题是我在 :param 变量周围有单引号。

也就是说,带有“{0}”的顶级查询不起作用。此外,以下 linq 查询不起作用。
var set = ctx.Orders.Where(a => a.OrderCode == orderCode); // Gets zero results.

当我对值进行硬编码时,它会正常工作并正确获取结果。
var set = ctx.Orders.Where(a => a.OrderCode == "XYZ"); // Gets the results correctly.

更新 2:
查询与 Devart 的 dotconnect 驱动程序一起使用。看起来这是 odp.net 的问题。

有人有类似的问题吗?

最佳答案

不确定您是否截断了示例,但如果您使用多个参数,这可能是问题所在:

Parameterized query in Oracle trouble

Although I can't see anything wrong with your example, I wonder if you're being hit by the old BindByName problem. By default, ODP.NET binds parameters to the query in the order in which they are added to the collection, rather than based on their name as you'd like. Try setting BindByName to true on your OracleCommand object and see if that fixes the problem.

关于linq - 使用 odp.net 的 Oracle Entity Framework 不采用 linq 查询中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17097243/

相关文章:

oracle - 对 PUBLIC 数据库链接表的托管 ODP.NET 调用导致 TNS 错误

C# - 使用 LINQ 对包含键/值对的列表框进行排序

c# - 在 linq 中连接字符串时获取 null

c# - 如何在linq中获得不同的值(value)

c# - 使用 LINQ 在 ASP.NET MVC 中传递数据 - 疯狂

c# - 如何使用 Asp.Net MVC C# 创建 CMS 应用程序

c# - 外键出现问题并使用 EF 插入记录

c# - 带有 bool 对象的odp net oracle参数构造函数

asp.net - 如何使用 asp.net Entity Framework 配置 mysql?

c# - 如何禁用连接池?