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.

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

 Database

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

Table

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

Model browser

First the Easy Way

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

Let's check how.

Open the edmx in the XML editor.

XML editor

Solution explorer

CRM

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.

Customer

Update Wizard

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

Checkboxes

After the Update, you will get the following error.

Error

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

Previous column

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

using (CRMEntities CRMObj = new CRMEntities())
{
    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.

Entity Framework

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.

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.

Procedure function

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

 Delete

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

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

Usp_GetCustomerID

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

using (CRMEntities CRMObj = new CRMEntities())
{
    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.

Go to the next part 6


Recommended Free Ebook
Similar Articles