0
var list = db.SomeModels.FromSql("select id from table1 where rownum < :r",10).ToList();

给我

System.ArgumentException:'无效的参数绑定参数名称:r'

我错过了什么。找不到任何文档

4

1 回答 1

0

以下应该有效:

var row = new OracleParameter("r", OracleDbType.Int32);
row.Value = 10;

var list = db.SomeModels
    .FromSql("select id from table1 where rownum < :r", new object[] { row })
    .ToList();

要添加多个参数,OracleParameter请在对象数组中添加更多对象:

var row = new OracleParameter("r", OracleDbType.Int32);
row.Value = 10;

var nameParameter = new OracleParameter("name", OracleDbType.Varchar2);
nameParameter.Value = "John";

var list = db.SomeModels
    .FromSql("select id from table1 where rownum < :r and firstname = :name", new object[] { row, nameParameter })
    .ToList();

不幸的是,像你自己一样,我还没有找到任何合适的文档。当我这样做时,我会编辑答案。

以下代码也可以工作(使用 Oracle.ManagedDataAccess.Core v2.19.31 和 Microsoft.EntityFrameworkCore v2.2.6 测试):

var list = db.SomeModels
    .FromSql("select id from table1 where rownum < {0}", 10)
    .ToList()
于 2019-08-02T17:56:27.693 回答