What is Entity Framework? Open Source Framework for ADO.NET
What is Entity Framework?
Entity Framework is a persistence framework which helps you to access the database and load your object and save it into the database.
Types of Workflows Using Entity Framework
There are 3 workflows to build domain mode using entity framework.
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 classes 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 procedures or triggers.
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 afterward.
Few Advantages of Code First
1. Full versioning of database
Using code first we can manage all versions of the database by using 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 afterward.
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 a single project here are a few commands which help you to migrate the database.
Few Tips For Migrations
1. Enable-Migrations
Enable-Migrations use only once in the entire project life.
2. ContextTypeName
If you want to deal with multiple DBContext you need to specify ContextTypeName.
PM> Enable-Migrations -ContextTypeName Namespace.ContextName1
3. 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
4. Add-Migration
Add migration command execute followed by comment and create migration script to update database changes
PM>Add-Migration comment
5. Configuration
The configuration is come into 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 a respected DBContext migration configuration file.
6. Update-Database
Update database command executes a script created by the add-migration command and finally reflects changes into the database.
Also if you are working with multiple DBContext you need to specify the configuration file path followed by -configuration command
PM> Update-Database -Configuration Namespace.configuration
The configuration.cs file contains the seed() function where you can make the default database entry.
If you having difficulty between which workflow to choose for development or having a hard time managing multiple DBContext feels free to contact us for the same. With our extensive expertise as a leading ASP.Net web and application development company, OneClick is fully equipped to provide you with exceptional services encompassing the design and development of your applications.