DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Use Migration With Entity Framework Core

How to Use Migration With Entity Framework Core

Learn how to utilize the migrations of Entity Framework Core RC2, complete with screen shots and code, from Jalpesh Vadgama.

Jalpesh Vadgama user avatar by
Jalpesh Vadgama
·
Jun. 21, 16 · Tutorial
Like (1)
Save
Tweet
Share
6.06K Views

Join the DZone community and get the full member experience.

Join For Free

Entity framework core is the lightweight, extensible, and cross-platform version of Entity Framework. After some time, Microsoft had released a new version of Entity Framework RC2. I have written a couple of blog posts about Entity framework code first migration (earlier for Entity framework 6.0). So there was a couple of requests coming for me to write a blog post about Entity Framework Core RC2 migration. So I thought it would be a good idea to give an overview how database migration works in Entity Framework Core RC2. This post will cover a basic scenario where we are going to create the database with existing ASP.NET Identity migration and then we are going to create a new model and have that migration applied in the database.

How to Use Entity Framework Migrations

Let’s get started. To demonstrate entity framework core migrations, I am going to create a sample asp.net core web application like following.

creating-project-core-migration

Once we select asp.net core application, it will appear the following dialog.

web-application-aspnet-core

Now create a sample application. It will basically create a boilerplate code for the asp.net identity and as a part of that it is going to create entity framework migration files under Data –> Migrations folder.

default-migration-entity-framework-core

Here you can find that sample code in GitHub repository given at the bottom. Now we already asp.net identity migration code ready. So let’s have those migrations applied with the following command from NuGet package manager console.

update-database

core-migration-aspnet-identity

Now let’s add a new model “Employee” like the following:

namespaceCoreMigration.Models{
    publicclassEmployee{
        publicintEmployeeId { get; set; }
        publicstringFirstName { get; set; }
        publicstringLastName { get; set; }    }}


As we have employee class, we need to add migration for that. I’m going to create a migration for employee class via the following command.

add-migration AddEmployee

add-employee-migration

It will create “AddEmployee” migration class in Data->Migrations folder.

employee-migration-in-solution-explorer

And here is the code for the migration for the same.

usingMicrosoft.EntityFrameworkCore.Migrations;
usingMicrosoft.EntityFrameworkCore.Metadata;   
namespaceCoreMigration.Data.Migrations{    
    publicpartialclassAddEmployee : Migration    {        
        protectedoverridevoidUp(MigrationBuilder migrationBuilder)        {   
            migrationBuilder.CreateTable(name: "Employees", columns: table => new{
                EmployeeId = table.Column<int>(nullable: false).Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                FirstName = table.Column<string>(nullable: true), LastName = table.Column<string>(nullable: true)},
            constraints: table => {
                table.PrimaryKey("PK_Employees", x => x.EmployeeId);
                });
            }
        protectedoverridevoidDown(MigrationBuilder migrationBuilder){
            migrationBuilder.DropTable(name: "Employees");
            }    
        }
    }


Now we have our migration class ready, so I am going to run the update-database command as following:

update-database-employee-migration-entity-framework-core

Now let’s check the database again. You can see employee table is there.

database-after-employee-migration

That’s it. It’s very easy. Hope you like it. Stay tuned for more.

Entity Framework Database Framework

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Development Trends 2023
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • ChatGPT — The Google Killer? The Job Taker? Or Just a Fancy Chatbot?
  • Too Many Tools? Streamline Your Stack With AIOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: