5 MVC实体框架6执行存储过程 [英] MVC 5 Entity Framework 6 Execute Stored Procedure

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

问题描述

我卡住了。我有一个非常庞大的数据库和存储过程和函数藏书丰富的图书馆现有的应用程序。所有我想要做的就是使用的DbContext执行存储过程并返回一组数据或映射到上下文中的实体之一。是神奇的东西我还没有发现在网络上的某个地方?一个人,任何人,请帮助。下面就是我这么远(和它不返回任何东西,结果是-1):

I'm stuck. I have an existing application with an extremely large database and extensive library of stored procedures and functions. All I want to do is use a DbContext to execute a stored procedure and return a set of data or map to one of the entities in the context. Is that something magical I haven't discovered on the net somewhere? Someone, anyone, please help. Here's what I've got so far (and it doesn't return anything, the result is -1):

var contacts = db.Database.ExecuteSqlCommand("Contact_Search @LastName, @FirstName",
    new SqlParameter("@LastName", GetDataValue(args.LastName)),
    new SqlParameter("@FirstName", GetDataValue(args.FirstName)));

执行时返回-1。我也尝试过的东西这样的效果没有成功:

Executing that returns -1. I also tried something to the effect of this with no success:

DbRawSqlQuery<Contact> data = db.Database.SqlQuery<Contact>
                                   ("EXEC Contact_Search @LastName, @FirstName",
                                       GetDataValue(args.LastName), 
                                       GetDataValue(args.FirstName));

我明白,我可以添加一个EDMX并映射到这样一个存储过程,但事实并非preferred方法。同样,我们的数据库包含了近450万条记录和近3000存储过程和函数库。这将是保持一个噩梦。上午我甚至开始朝着正确的方向?是实体框架是正确的选择?

I understand that I could add an edmx and map to a stored procedure that way, but that is not the preferred method. Again, our database contains nearly 450 million records and a library of almost 3,000 stored procedures and functions. It would be a nightmare to maintain. Am I even starting in the right direction? Is Entity Framework the right choice?

推荐答案

哇,看来之后我放弃了,我莫名其妙地偶然发现了答案。我发现了一个<一个href=\"http://www.dotnetthoughts.net/how-to-execute-a-stored-procedure-with-entity-framework-$c$c-first\"相对=nofollow> FANTASTIC帖子有关执行存储过程和阅读后,这是我的解决方案:

Wow, it seems right after I give up, I somehow stumble upon the answer. I found a FANTASTIC post about executing stored procedures and after reading up, this was my solution:

var contacts = db.Database.SqlQuery<Contact>("Contact_Search @LastName, @FirstName",

所以,非常感谢Anuraj他出色的帖子!我的解决方案的关键是首先使用类SqlQuery 而不是 ExecuteSqlCommand ,也执行该方法映射到我的实体模型(联系)。

So, many thanks to Anuraj for his excellent post! The key to my solution was to first use SqlQuery instead of ExecuteSqlCommand, and also to execute the method mapping to my entity model (Contact).

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

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