如何使用EntityFramework调用存储过程?

Ivy*_*Ivy 35 c# mysql stored-procedures entity-framework

我已经从MySQL数据库生成了EF4模型,并且我已经包含了StoredProcedures和Tables.

我知道如何针对EF进行定期检查/更新/获取/删除操作,但我找不到我的StoredProcedures.

这就是我所希望的:

using (Entities context = new Entities())
{
    context.MyStoreadProcedure(Parameters); 
}
Run Code Online (Sandbox Code Playgroud)

编辑1:

这是没有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));
Run Code Online (Sandbox Code Playgroud)

PS.如果需要,我可以更改EF版本

编辑1:

CREATE DEFINER=`106228`@`%` PROCEDURE `updateGame`(
    inId INT,
    inName VARCHAR(255),
    inDescription TEXT,
    inYearPublished DATETIME,
    inMinPlayers INT,
    inMaxPlayers INT,
    inPlayingTime VARCHAR(127)
)
Run Code Online (Sandbox Code Playgroud)

Qui*_*rdt 66

一种方法是使用DbContext下的Database属性:

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

EF5绝对支持这一点.

  • 是的,如果您还没有将它链接到您的模型并且您想要 _raw_ 访问,这就是这样做的方法;) (2认同)
  • 这只是执行存储过程作为非查询.它无法读取任何返回的表 (2认同)

小智 10

您已使用SqlQuery函数并指示要映射结果的实体.

我发送一个例子来执行此操作:

var oficio= new SqlParameter
{
    ParameterName = "pOficio",
    Value = "0001"
};

using (var dc = new PCMContext())
{
    return dc.Database
             .SqlQuery<ProyectoReporte>("exec SP_GET_REPORTE @pOficio",
                                        oficio)
             .ToList();
}
Run Code Online (Sandbox Code Playgroud)


ESG*_*ESG 5

将存储过程导入模型后,可以右键单击它(从模型浏览器的Context.Store/ Stored Procedures部分),然后单击Add Function Import。如果需要使用复杂类型,则可以在此处创建它。


tam*_*tam 1

这就是我最近为我的数据可视化应用程序所做的,该应用程序具有 2008 SQL 数据库。在此示例中,我收到从存储过程返回的列表:

public List<CumulativeInstrumentsDataRow> GetCumulativeInstrumentLogs(RunLogFilter filter)
    {
        EFDbContext db = new EFDbContext();
        if (filter.SystemFullName == string.Empty)
        {
            filter.SystemFullName = null;
        }
        if (filter.Reconciled == null)
        {
            filter.Reconciled = 1;
        }
        string sql = GetRunLogFilterSQLString("[dbo].[rm_sp_GetCumulativeInstrumentLogs]", filter);
        return db.Database.SqlQuery<CumulativeInstrumentsDataRow>(sql).ToList();
    }
Run Code Online (Sandbox Code Playgroud)

然后在我的例子中使用这个扩展方法进行一些格式化:

public string GetRunLogFilterSQLString(string procedureName, RunLogFilter filter)
        {
            return string.Format("EXEC {0} {1},{2}, {3}, {4}", procedureName, filter.SystemFullName == null ? "null" : "\'" + filter.SystemFullName + "\'", filter.MinimumDate == null ? "null" : "\'" + filter.MinimumDate.Value + "\'", filter.MaximumDate == null ? "null" : "\'" + filter.MaximumDate.Value + "\'", +filter.Reconciled == null ? "null" : "\'" + filter.Reconciled + "\'");

        }
Run Code Online (Sandbox Code Playgroud)