MVC 5实体框架6执行存储过程 [英] MVC 5 Entity Framework 6 Execute Stored Procedure
问题描述
var contacts = db.Database.ExecuteSqlCommand(Contact_Search @LastName,@FirstName,
new SqlParameter(@ LastName,GetDataValue(args.LastName)),
new SqlParameter(@ FirstName,GetDataValue args.FirstName)));
执行返回-1。我也尝试了一些效果,没有成功:
DbRawSqlQuery< Contact> data = db.Database.SqlQuery< Contact>
(EXEC Contact_Search @LastName,@FirstName,
GetDataValue(args.LastName),
GetDataValue(args.FirstName));
我知道我可以添加一个edmx并映射到一个存储过程,但这不是优选的方法。再次,我们的数据库包含近4.5亿条记录和一个近3000个存储过程和功能的库。这将是一场噩梦维持。我甚至从正确的方向开始吗?实体框架是正确的选择吗?
哇,似乎在我放弃之后,我以某种方式绊倒了答案。我发现一个 FANTASTIC post 关于执行存储过程和阅读后,这是我的解决方案:
var contacts = db.Database.SqlQuery< Contact> ;(Contact_Search @LastName,@FirstName,
所以,非常感谢Anuraj的出色的帖子!我的解决方案的关键是首先使用 SqlQuery
而不是 ExecuteSqlCommand
,并执行方法映射到我的实体模型(联系人
)。
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)));
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));
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?
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",
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
).
这篇关于MVC 5实体框架6执行存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!