How to use stored procedure in ADO.NET Entity Framework?
如何在ADO.NET Entity Framework中使用存储过程?
My Table : MyCustomer
我的表: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.
数据读取器与指定的“TestAdonetEntity2Model.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.
添加就可以了。每个添加的过程都可以。但编辑后,会出现上述错误。
4 个解决方案
#1
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
如何:导入存储过程
#2
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
#3
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 Operations(Transactions),您可以使用
_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类
#4
try
{
entityContext.MyCustomerAdd(textName.Text.Trim(), textSurName.Text.Trim())
}
catch (Exception ex)
{
;
}
Added Process runs correctly. On the other hand ; after added process, give Above error.But My solution run correct!!!
添加的流程正确运行。另一方面 ;添加进程后,给出Above错误。但我的解决方案运行正确!
#1
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
如何:导入存储过程
#2
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
#3
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 Operations(Transactions),您可以使用
_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类
#4
try
{
entityContext.MyCustomerAdd(textName.Text.Trim(), textSurName.Text.Trim())
}
catch (Exception ex)
{
;
}
Added Process runs correctly. On the other hand ; after added process, give Above error.But My solution run correct!!!
添加的流程正确运行。另一方面 ;添加进程后,给出Above错误。但我的解决方案运行正确!