Get a Quote


Entity Framework in Depth

oneclick, administrator/ April 25, 2019 5:48 pm 404 Views

Entity Framework

What is Entity Framework?

Entity Framework is a persistence framework which helps you to access database and load your object and save into the database.

Entity Framework1

Workflows

There are 3 workflows to build domain mode using entity framework.

Workflow

Most developers use the Database First and Code First approach to developing software.

            1. Database First Workflow

Database-first development workflow generates the context and entities for the existing database using the EDM wizard integrated into Visual Studio or executing EF commands.

            2. Code First Workflow

Use this workflow when you don’t have any legacy database for your application. In the code-first approach, first, need to write entities (domain classes) and context class and then create the database from these classes using migration commands.

            3. Model First Workflow

In the model-first approach, first need to create entities, relationships, and inheritance hierarchies directly on the visual designer integrated into Visual Studio after that generate entities, context class, and the database script from your visual model.

Many developers have misconceptions about the Database First approach

Database First gives you more control over the database: which is not true at all. With Code First, you have full control over the database, for example, you can create and remove stored procedure or trigger.

Code First is for greenfield projects: which is also not true. You can use reverse engineering your existing database to create the code first model and then use code first migration for any subsequent changes afterword.

In my opinion here are Few advantages of code first

         1. Full versioning of database

Using code first we can manage all version of the database by use of single comment which is very helpful while maintaining different versions of the application. Without code first, you either need to manually take care of database versions or need to use an external tool.

         2. Productivity

Even you can use code first workflow with legacy database also, by the help of reverse engineering your legacy database to create code first model and then use code first migration for any subsequent changes afterword.

For reverse engineering your legacy database Entity Framework provides Code First From Database entity data model wizard.

You can also deal with multiple DBContext into the single project here are a few commands which help you to migrate the database.

Few Tips For Migrations

  • Enable-Migrations

Enable-Migrations use only once in entire project life.

  • ContextTypeName

If you want to deal with multiple DBContext you need to specify ContextTypeName.

PM> Enable-Migrations -ContextTypeName Namespace.ContextName1

  • MigrationsDirectory

At the time of enabling another migration for another DBContext also need to provide MigrationsDirectory.

PM> Enable-Migrations -ContextTypeName Namespace.ContextName2 -MigrationsDirectory : Migrations\DirName

  • Add-Migration

Add migration command execute followed by comment and create migration script to update database changes

PM>Add-Migration comment

  • Configuration

The configuration is come to in picture when we enable migration it will create a configuration file.

Configuration command use when we are dealing with multiple DBContext just like below

PM>Add-Migration comment -Configuration Namespace.configuration

Namespace.configuration is a path of respected DBContext migration configuration file.

  • Update-Database

Update database command executes a script created by add-migration command and finally reflect changes into the database.

Also if you are working with multiple DBContext you need to specify configuration file path followed by -configuration command

PM> Update-Database -Configuration Namespace.configuration

The configuration.cs file contains seed() function where you can make default database entry.

If you having difficulty between which workflow choose for development or having a hard time to manage multiple DBContext feels free to contact us for the same. My pleasure to help you out.