C# 如何在 ADO.NET Entity Framework 中运行存储过程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/991397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:03:56  来源:igfitidea点击:

How do I run a stored procedure in ADO.NET Entity Framework?

c#.netlinq-to-sqlentity-framework

提问by Penguen

How to use stored procedure in ADO.NET Entity Framework?

如何在 ADO.NET Entity Framework 中使用存储过程?

My Table : MyCustomer

我的表:我的客户

Columns:
CustomerID    PK   int 
Name               nvarchar(50)
SurName            nvarchar(50)

My stored procedure

我的存储过程

ALTER procedure [dbo].[proc_MyCustomerAdd]
(@Name nvarchar(50),
@SurName nvarchar(50)
)
as 
begin
  insert into dbo.MyCustomer([Name], SurName) values(@name,@surname)
end

My C# code

我的 C# 代码

private void btnSave_Click(object sender, EventArgs e)
{
   entityContext.MyCustomerAdd(textName.Text.Trim(), textSurName.Text.Trim());
   entityContext.SaveChanges();
}

The error:

错误:

The data reader is incompatible with the specified 'TestAdonetEntity2Model.MyCustomer'. A member of the type, 'CustomerID', does not have a corresponding column in the data reader with the same name.

数据读取器与指定的“Te​​stAdonetEntity2Model.MyCustomer”不兼容。'CustomerID' 类型的成员在数据读取器中没有具有相同名称的对应列。

Error occured below the last code line (call to ExecuteFunction):

在最后一行代码下方发生错误(调用 ExecuteFunction):

global::System.Data.Objects.ObjectParameter surNameParameter;
if ((surName != null))
{
   surNameParameter = new global::System.Data.Objects.ObjectParameter("SurName", surName);
}
else
{
   surNameParameter = new global::System.Data.Objects.ObjectParameter("SurName", typeof(string));
}
<b>return base.ExecuteFunction<MyCustomer>("MyCustomerAdd", nameParameter, surNameParameter);</b>

Added is ok. Every added process is ok. But after editing, above error occurs.

添加好了。每个添加的过程都可以。但编辑后,出现上述错误。

采纳答案by Chathuranga Wijeratna

I think what you need to do it a function import with the EF tooling and calling the imported function like

我认为您需要使用 EF 工具进行函数导入并调用导入的函数,例如

DataContext.MyFunctionName(storedProcedureParamer1, storedProcedureParamer2)

How to: Import a Stored Procedure

如何:导入存储过程

回答by marc_s

Just a wild guess (I haven't used EF with stored procs): wouldn't the name of the function used in "ExecuteFunction" have to be the same as the stored proc's name??

只是一个疯狂的猜测(我没有将 EF 与存储过程一起使用):“ExecuteFunction”中使用的函数名称是否必须与存储过程的名称相同??

return base.ExecuteFunction("MyCustomerAdd", nameParameter, surNameParameter);

ALTER procedure [dbo].[proc_MyCustomerAdd]

Can you try to use:

你可以尝试使用:

return base.ExecuteFunction("proc_MyCustomerAdd", nameParameter, surNameParameter);

Does that make any difference?

这有什么区别吗?

Marc

马克

回答by karthik kasubha

To call Stored Procedures for query operations you can use SqlQuery in Entityframework which is very helpful

要调用存储过程进行查询操作,您可以在 Entityframework 中使用 SqlQuery,这非常有用

_dbContext.Database.SqlQuery<EntityType>("sp_name",parameters).ToList();

For Executenonquery Operations(Transactions) you can use

对于 Executenonquery 操作(事务),您可以使用

_dbContext.Database.ExecuteSqlCommand( 
                    @"UPDATE tblname SET Rating = 5" + 
                        " WHERE Name LIKE '%Entity Framework%'" 
                    );

Please note that _dbContext object of your Context class inherits from DbContext Class of Entityframework

请注意,您的 Context 类的 _dbContext 对象继承自 Entityframework 的 DbContext 类