Sql存储过程和实体框架6 [英] Sql Stored proc and Entity framework 6

查看:178
本文介绍了Sql存储过程和实体框架6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个参数存储过程:

Suppose i have a one parameter stored procedure :

procedure dbo.MyStoredProc @param VARCHAR(50)
as 
SELECT *
FROM whatever
WHERE column = @param

在我的Web应用程序(mvc与实体6 ),我希望调用我的存储过程,并从它的 SELECT 语句中检索所有行。 / p>

In my web application (mvc with entity 6) i wish to call my stored procedure, and retreive all the line from it's SELECT statement.

 var DBLines = MyStoredProc(parameterValue);

我看到一些令人失望但非常逻辑的东西; 我的var包含一个整数。该整数为1.它告诉我,在我的存储过程中一切顺利...

I saw something disappointing but very logic; my var was containing an integer. That integer was 1. It was telling me that everything went ok in my stored procedure...

我希望var到包含我存储的proc经过的每一条SQL行。我想这是可能的,但如何呢?必须有一个公共逻辑。所有我在互联网上看到的都是解决方法。

I want that var to contains every SQL lines my stored proc went through. I suppose it is possible but how ? There must be a commun logic. All i saw on internet... was workarounds.

推荐答案

经过很多挫折,我找到了一种方式,周围。这个对我来说很好,而且它不需要额外的C#代码,没有模型,没有这样的东西,例如 public class FunctionsContext:BaseContext< FunctionsContext> {} 那就是...

After lots of frustrations, i found a way, but it's another work around. This one works pretty well for me though, and it does not require extra C# code, no model, and no such things as public class FunctionsContext : BaseContext<FunctionsContext>{} whatever that is...

在SQL Server中,我创建了一个空表,就像我的存储过程的返回一样。假设我的存储过程是返回:

In SQL Server, i created an empty table that looks just like the return of my stored procedure. Suppose my stored procedure was to return :

SELECT nvarchar(2), int, datetime FROM whatever WHERE <condition>

然后我的Empty_Table使用相同的var类型创建。在将我的Empty_Table与实体框架映射向导映射之后。我可以使用它作为一种类型:

Then my Empty_Table is created with the same var types. After mapping my Empty_Table with Entity framework mapping wizard. I could use it as a type to to this :

databaseEntities db = new databaseEntities();

var query = db.Database.SqlQuery<Empty_Table>("EXEC [dbo].[My_Stored_Proc] @p1", new SqlParameter("p1", parameterValue));

foreach (var line in query)
{
    int i = line.ID
}

它工作得很好。这将是使用实体使用存储过程的主要(唯一)技术。

And it worked perfectly. This is going to be my main (only) technique for using stored procedure with Entity.

这篇关于Sql存储过程和实体框架6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆