Popular Object-Relational Mapping Framework for .NET applications ,uses Database First and Code first two different approaches in Entity Framework . They represent two different ways for creating and managing your database schema and data model. Entity Framework Database first approach allows developers to generate Entity Data Model (EDMX) and entity classes from an already existing database. When a new column is added to the database table , it’s crucial to update the EDMX model to contain the changes in your Application. In this blog we are going to know that , “How to add a new Column to Entity Framework EDMX Model in Database first Approach”.

Why Entity Framework EDMX file important

With agile development, product development goes through continuous improvement and adapt to the market needs. Few changes require new fields to be added or updated in the database. This blog covers how to add a new table field and use it in the Microsoft .net core entity framework. In the Database First approach , you can use an existing Database and start generating a new Entity Data Model (EDMX) from it. EDMX file is a visual representation of database schema and its relationships. So that developers can easily understand the structure of the database . Entity framework then creates classes based on tables available in the database .This approach is very useful when you have already created a database and want to generate a Data Model.

How to add a New Column to Entity Frame Work EDMX Model in Database First Approach

Update the EDMX Model from Database : To update the EDMX model with new field following are the steps :

1. Let’s add one new field in the user table . Using the following query in sql server.

Syntax: ALTER TABLE Table_Name ADD column_name data_type [constraints]; Example: ALTER TABLE User ADD LogCount int ;

2. Open your project in visual studio and locate the EDMX file in the Solution Explorer.

3. Right-click on the EDMX file and select “Open With ” => Entity Data Model Designer.

4. In the EDMX Designer file , right click on empty space , and select option “Update Model from Database”.

5. Now, the Update window will open , displaying a list of available database objects to update.

6.Click on Add as you added a new column in the database . Check the checkbox corresponding to the table where you added a new column .

7. Click on Refresh and click “Finish”.

8. Entity framework will now update the EDMX model with the changes from the database , including new columns.

Summary

In this blog, we covers steps to add column  in  the enitiy framework EDMX model in database first approach.

For more information & quries about entity framework please visit this link

Patterns7 Technologies is helping customers define digital transformation roadmap and NoSQL databases helps to build scalable solutions. Read more at on our product engineering services.