In this article, We learn how to create or generate Migration Script. I will show how to do it, and just follow the steps clearly.
For more updates please do Subscribe via Email:
Entity Framework Migration?
Before we dive in to that topic we need to know what is EF and Migration. Entity Framework(EF) is an open-source ORM framework for . NET applications supported by Microsoft. It enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored.
Migration is a way to keep the database schema in sync with the EF Core model by preserving data. As per the above figure, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model.
Entity Framework Migration Script
EF Migration Script is a SQL script file runnable in MSSQL server or in your SQL Server Management Studio (SSMS) application. For the good and best practice in developing web and application you need to do and generate Migration Script.
Reason why we do this.
You cannot update directly in SQL server about your changes is table and other field, specially your application is a code first approach. WHY? because your code is not publish and the DB is updated eventually the application will crashed. So you need to generate migration script, Your changes will be apply to the live DB when your changes pull request approved and completely merge and release.
How to do that?
Note: In this article I’ll used code first approach;
First we must have Model for the code fist approach.
public class Student
{
[Key]
public int Id { get; set; }
[Column(TypeName = "varchar(50)")]
public string StudentCode { get; set; }
[Required]
[Column(TypeName = "varchar(50)")]
public string S_FirstName { get; set; }
[Required]
[Column(TypeName = "varchar(50)")]
public string S_LastName { get; set; }
[Column(TypeName = "varchar(50)")]
public string S_MiddleName { get; set; }
public int StudentLevel { get; set; }
[Column(TypeName = "varchar(50)")]
public string StudentContacts { get; set; }
[Column(TypeName = "varchar(50)")]
public string StudentEmail { get; set; }
[Column(TypeName = "varchar(50)")]
public string StudentAdviser { get; set; }
public DateTime TDT { get; set; }
public DateTime UDT { get; set; }
}
Code Github Link: JaveTLupango/StudentInfoAPI/Model
As we can see here, Student table is not available.
To create table base on the model. We need to add first the migration.
PM> add-migration
Name: CreateStudentTable
Or you can do like this.
PM> Add-migration CreateStudentTable -context StudentDBContext
After adding migration, this file will be available in you code solution folder.
Generate EF Migration Script
PM> Update-Database -Script
And it will generate a SQL Script after you run this command in your CLI or if you are using Microsoft Visual Studio you can run in the Package Manager Console (PM).
I will explain further why you need to know this approach and a developer.
This approach is necessary if you are working with a big project and more than one environment need to go to proceed to a live or production environment. Because developers and programmers works and doing changes in dev environment with separate resources to uat and production environment.
When the business case or business role/logic is completed by the programmers or developers its will be push and Pull requested to next environment for another steps of the validation processes. To implement the any changes in database in uat you need to run the script generated by the developers or programmers during the development journey. That’s why we need to be aware this kind of approach. Not only for the uat but also during the deployment process in production.
If you like this kind of discussion just subscribe via email here:
Happy Learning..
Thank you for visiting my blog site. Hoping you learn more here. please feel free to comment and suggest if there is need to enhance and update. thank you.
Related Topics
How to Generate Script In Entity Framework Migration
In this article, We learn how to create or generate Migration Script. I will show how to do it, and just follow the steps clearly. […]
How to use Stored Procedure using Entity Framework in .Net Core
In this article, We can learn how to consume Stored Procedure (SP) using Entity Framework in .Net Core. I will show you how to do […]
Cannot be loaded because its operation is blocked by software restriction policies, such as those created by using Group Policy
In this article we learn how to address if you encounter this issue in your development journey. I will show you how, Just follow the […]
.NET Core API using Entity Framework (EF) Code First Approach
In this article, I will show how to create .NET Core API using Entity Framework Code First Approach. I will develop Student Web API application […]
ASP.Net CRUD using Entity Framework
In this tutorial, I create a project in ASP.net CRUD Web Application using Entity Framework. This Project user can Create, Read(Display), Update, Delete data from Database using […]