如何调用带的EntityFramework存储过程? [英] How to call Stored Procedures with EntityFramework?

查看:153
本文介绍了如何调用带的EntityFramework存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经genereated从MySQL数据库的EF4型号,我既包括StoredProcedures和表。

我知道如何让普通instert /更新/读取/删除操作对EF,但我不觉得我的StoredProcedures?

这是我希望脱颖而出:

 使用(实体上下文=新的实体())
{
    context.MyStoreadProcedure(参数);
}

编辑1:

这是它的样子,而不EF:

  sqlStr =(???????,,,,,,)CALL updateGamecommandObj =新的OdbcCommand(sqlStr,mainConnection);
commandObj.Parameters.Add(@ ID,OdbcType.Int).value的= inGame.id;
commandObj.Parameters.Add(@名,OdbcType.VarChar,255).value的= inGame.name;
commandObj.Parameters.Add(@描述,OdbcType.Text).value的=; //inGame.description;
commandObj.Parameters.Add(@ yearPublished,OdbcType.DateTime).value的= inGame.yearPublished;
commandObj.Parameters.Add(@ minPlayers,OdbcType.Int).value的= inGame.minPlayers;
commandObj.Parameters.Add(@ MAXPLAYERS,OdbcType.Int).value的= inGame.maxPlayers;
commandObj.Parameters.Add(@ playingTime,OdbcType.VarChar,127).value的= inGame.playingTime;返回Convert.ToInt32(executeScaler(commandObj));

PS。如果需要的话我可以改变EF版本

编辑1:

  CREATE DEFINER =`106228` @``%`程序updateGame`(INID INT,inName VARCHAR(255)inDescription TEXT,inYearPublished DATETIME,inMinPlayers INT,inMaxPlayers INT,inPlayingTime VARCHAR(127))


解决方案

一种方法是使用数据库属性关闭的DbContext:

 的SqlParameter参数1 =新的SqlParameter(@名字,弗兰克);
SqlParameter的参数2 =新的SqlParameter(@姓氏,Borland公司);
context.Database.ExecuteSqlCommand(sp_MyStoredProc @firstName,@LastName
                              参数1,参数2);

EF5绝对支持。

I have genereated a EF4 Model from a MySQL database and I have included both StoredProcedures and Tables.

I know how to make regular instert/update/fetch/delete operations against the EF but I dont find my StoredProcedures?

This was what I was hoping fore :

using (Entities context = new Entities())
{
    context.MyStoreadProcedure(Parameters); 
}

Edit 1:

This is how it looked without the EF :

sqlStr = "CALL updateGame(?,?,?,?,?,?,?)";

commandObj = new OdbcCommand(sqlStr, mainConnection);
commandObj.Parameters.Add("@id,", OdbcType.Int).Value = inGame.id;
commandObj.Parameters.Add("@name", OdbcType.VarChar, 255).Value = inGame.name;
commandObj.Parameters.Add("@description", OdbcType.Text).Value = ""; //inGame.description;
commandObj.Parameters.Add("@yearPublished", OdbcType.DateTime).Value = inGame.yearPublished;
commandObj.Parameters.Add("@minPlayers", OdbcType.Int).Value = inGame.minPlayers;
commandObj.Parameters.Add("@maxPlayers", OdbcType.Int).Value = inGame.maxPlayers;
commandObj.Parameters.Add("@playingTime", OdbcType.VarChar, 127).Value = inGame.playingTime;    

return Convert.ToInt32(executeScaler(commandObj));

PS. I can change EF version if needed

Edit 1:

CREATE DEFINER=`106228`@`%` PROCEDURE `updateGame`(

inId INT,

inName VARCHAR(255),

inDescription TEXT,

inYearPublished DATETIME,

inMinPlayers INT,

inMaxPlayers INT,

inPlayingTime VARCHAR(127)

)

解决方案

One way is to use the Database property off the DbContext:

SqlParameter param1 = new SqlParameter("@firstName", "Frank");
SqlParameter  param2 = new SqlParameter("@lastName", "Borland");
context.Database.ExecuteSqlCommand("sp_MyStoredProc @firstName, @lastName", 
                              param1, param2);

EF5 definitely supports that.

这篇关于如何调用带的EntityFramework存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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