简体   繁体   中英

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

How to use stored procedure in 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

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.

Error occured below the last code line (call to 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

DataContext.MyFunctionName(storedProcedureParamer1, storedProcedureParamer2)

How to: Import a Stored Procedure

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

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

For Executenonquery Operations(Transactions) you can use

_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

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??

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!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM