Practical Introduction To Entity Framework: Day 5

The following are my previous articles on the fundamentals of the Entity Framework:

To start with this article please read all 4 previous articles to understand better or see this video https://www.youtube.com/v/b6vTIiBNcJ0

I am assuming you are good in Entity Framework now, so let's start.

How to update column and selected query data of Stored Procedure in Entity Framework

Many developers know how to add an entity and Stored Procedure but they don't know how to update.

If I changed the column name of a customer table then how to update it in the Entity Framework?

Please go through the following step-by-step, it's very easy.

A database structure that I used in this project is shown in the following screen.



Create a normal table and Stored Procedure in a database then add a table entity in Entity Framework as below.



Check the entity in the Model Browser after double-clicking the edmx.



First the Easy Way

We can update it the easy way; just replace "Id" (the column name) to "CustID" in the edmx XML file.

Let's check how.

Open the edmx in the XML editor.







Just replace all values, and it's done.

You can run and check that it works.

Now for the second way to update the same thing.

Second Way With Options

Choose the update model from the database options to update.





Uncheck the checkboxes as in the following if you have used a foreign key.



After Update you will get the following error.



This error is because the previous column is also present. So delete that column as in the following.



And it is done. Just call the following code and check that your value is being got properly.

  1. using (CRMEntities CRMObj = new CRMEntities())  
  2. {  
  3.      var CustomerEntity = CRMObj.Customers;  

Now, the next question is how to update the select query data of the Stored Procedure.

Add a normal Stored Procedure in the Entity Framework as in the following.



Automatically complex type created.

If I changed the select query schema after adding this Stored Procedure in the Entity Framework then we can use the replace option using the XML editor.

Or the second way with options is to check the procedure function as in the following. If it is the same as created then remove that complex type and create a new one, else update in the complex type.

First check the procedure function as in the following:



If it is the same as created previously then delete it as in the following:



To create a new complex type then double-click on the Function Import -> Usp_GetCustomerID.

Get the column information and check that you updated the schema and create a new complex type as in the following:



We have done it. Just run the following code to check the procedure output with the new schema.

  1. using (CRMEntities CRMObj = new CRMEntities())  
  2. {  
  3.       var CustomerProcedureEntity = CRMObj.USP_GetCustomerByID(1);  

We can do it with an update of the complex type but it is always better to delete and add.

Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all