gpt4 book ai didi

c# - 如何使用 EntityFramework 调用存储过程?

转载 作者:IT老高 更新时间:2023-10-28 23:41:40 25 4
gpt4 key购买 nike

我从 MySQL 数据库生成了一个 EF4 模型,并包含了 StoredProcedures 和 Tables。

我知道如何对 EF 进行常规的插入/更新/获取/删除操作,但我找不到我的 StoredProcedures。

这是我所希望的:

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

编辑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));

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

编辑1:

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

最佳答案

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

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

EF5 绝对支持。

关于c# - 如何使用 EntityFramework 调用存储过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14264750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com