简体   繁体   English

如何从Entity Framework 6(数据库优先)和Npgsql执行存储过程?

[英]How to execute stored procedure from Entity Framework 6 (database first) and Npgsql?

Is this a bug? 这是错误吗?

My components are: 我的组件是:

  1. .NET 4.6.1 .NET 4.6.1
  2. Entity Framework 6.0 实体框架6.0
  3. Npgsql 3.0.5 Npgsql 3.0.5
  4. PostgreSQL 9.5 PostgreSQL 9.5

First, I created a table and stored procedure in PostgreSQL 9.5 首先,我在PostgreSQL 9.5中创建了一个表和存储过程

CREATE TABLE hello
(
  msg text
)
WITH (
  OIDS=FALSE
);
ALTER TABLE hello
  OWNER TO postgres;

CREATE OR REPLACE FUNCTION sayhello()
  RETURNS SETOF hello AS
$BODY$ 

select
 *
 from version()

  $BODY$
  LANGUAGE sql VOLATILE
  COST 100
  ROWS 1000;
ALTER FUNCTION sayhello()
  OWNER TO postgres;

Second, I went to the .edmx file (Entity Framework 6.0), choose "update from database", selected the new table "hello" and the new stored procedure, "sayhello". 第二,我转到.edmx文件(实体框架6.0),选择“从数据库更新”,选择新表“ hello”和新存储过程“ sayhello”。

The Model Browser now shows the new table entity and the imported function. 现在,模型浏览器显示新的表实体和导入的函数。

Third, add a new procedure to the WCF file: 第三,向WCF文件添加一个新过程:

public string SayHello()
{
            using (var ctx = new chaosEntities())
            {
                var x = ctx.sayhello();
                return "Hello";
            }
}

Set the WCF Service as Startup project and Start Debugging. 将WCF服务设置为“启动”项目并启动“调试”。

The WCF Test Client comes up. WCF测试客户端启动。 Executing SayHello() from the WCF Service leads to: 从WCF服务执行SayHello()会导致:

public virtual ObjectResult<hello> sayhello()
{
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<hello>("sayhello");
}

When this is executed, I get: 执行此操作后,我得到:

An exception of type 'System.Data.Entity.Core.EntityCommandCompilationException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型为'System.Data.Entity.Core.EntityCommandCompilationException'的异常,但未在用户代码中处理

Additional information: An error occurred while preparing the command definition. 附加信息:在准备命令定义时发生错误。 See the inner exception for details. 有关详细信息,请参见内部异常。

Inner Exception is: {"Value does not fall within the expected range."} 内部异常是:{“值不在预期范围内。”}

As I have several hundred stored procedures, any help on how to fix this is most appreciated. 由于我有数百个存储过程,因此,非常感谢有关如何解决此问题的任何帮助。

TIA TIA

Note: I suspect the problem is with NpgsqlServices.TranslateCommandTree, but I'm only guessing. 注意:我怀疑问题出在NpgsqlServices.TranslateCommandTree,但我只是在猜测。

I could never get it to work the way I hoped (via EntityFramework), so I ended up doing this. 我永远无法使它按我希望的方式工作(通过EntityFramework),所以我最终做到了。 I'd sure be open to a better solution! 我肯定会接受更好的解决方案!

The below code calls Npgsql directly to avoid the whole EntityFramework thing. 下面的代码直接调用Npgsql来避免整个EntityFramework。

public string SayHello()
        {
            using (var ctx = new chaosEntities())
            {
                var b = ctx.Database.Connection.ConnectionString;

                using (var conn = new Npgsql.NpgsqlConnection(connectionString: b))
                {
                    conn.Open();
                    using (var tran = conn.BeginTransaction())
                    using (var command = conn.CreateCommand())
                    {
                        command.CommandText = "sayhello";
                        command.CommandType = CommandType.StoredProcedure;

                        var g = (string)command.ExecuteScalar();
                        return g;
                    }
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架数据库中的存储过程第一种方法 - Stored procedure in Entity Framework database first approach 如何使用实体框架执行系统存储过程 - How to execute system stored procedure with entity framework 在Entity Framework数据库优先方法中,如何从存储过程返回多个结果集? - In Entity Framework database-first approach how to return multiple result sets from a stored procedure? 如何使用实体框架执行存储过程并从存储过程获取输出 - How to execute stored procedure and get output from the stored procedure using Entity Framework 在实体框架中执行存储过程 - Execute stored procedure in entity framework 具有不同返回类型的实体框架数据库优先存储过程 - Entity Framework Database First Stored Procedure with different return types 实体框架数据库首先将参数添加到存储过程 - Entity Framework Database first add parameter to stored procedure 首先使用Database.SqlQuery从Entity Framework Code调用存储过程不起作用 - Calling stored procedure from Entity Framework Code First with Database.SqlQuery is not working Oracle的存储过程不适用于Entity Framework数据库优先的工作流程 - Stored procedure from Oracle doesn't work with Entity Framework database-first workflow 如何使用返回值参数执行Entity Framework SQLQuery存储过程? - How to execute Entity Framework SQLQuery stored procedure with return value parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM