[英]How do I run a stored procedure in ADO.NET Entity Framework?
How to use stored procedure in ADO.NET Entity Framework? 如何在ADO.NET实体框架中使用存储过程?
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'.
数据读取器与指定的“ TestAdonetEntity2Model.MyCustomer”不兼容。 A member of the type, 'CustomerID', does not have a corresponding column in the data reader with the same name.
类型“ 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.
但是编辑后,会发生上述错误。
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)
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类。
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与存储的proc一起使用):“ ExecuteFunction”中使用的函数的名称不必与存储的proc的名称相同吗?
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 渣
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!!!
添加过程后,给上面的错误。但我的解决方案运行正确!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.