In ADO.NET Entity Framework, how can I start a stored procedure?

.net c# entity-framework linq-to-sql

Question

How can I utilize the ADO.NET Entity Framework's stored procedures?

My Table: My Client

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

I have a saved 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# program

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

The mistake

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.

Below the final line of code (the call to ExecuteFunction), there was an error:

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>

OK to add. Every new procedure is acceptable. However, the aforementioned problem appears after amending.

1
1
6/13/2009 9:04:50 PM

Accepted Answer

What you should do, in my opinion, is import a function using the EF tooling and call it something like

DataContext.MyFunctionName(storedProcedureParamer1, storedProcedureParamer2)

Implement a Stored Procedure

2
1/1/2014 1:20:04 PM

Popular Answer

SqlQuery in Entityframework may be used to invoke Stored Procedures for query operations, which is quite useful.

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

You may use Executenonquery Operations (Transactions) to do

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

Please be aware that your Context class's _dbContext object derives from the Entityframework DbContext Class.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow