EF (code first) Migration and Update to database

jwp profile image John Peters ・1 min read

EF Code First in Visual Studio

If you are using EF Code first, you already know about DBSETs and Entity Models. Adding or removing fields, changing namespaces, and altering property types in any Entity Model is common during the development cycle.

But how do you get these changes out to the database with a clean set of data?

Get a clean build first and then...

Add a Migration for the New Schema

Add-Migration XYZMigrationName 
  -Project XYZProjectName.Data
  -StartupProject XYZProjectName.Web.Api 
  -Context DBStore

Drop the Existing Database in SSMS

USE [master]
alter database [DBName] set single_user with rollback immediate

Udpate the Database

  -Project XYZProjectNsme.Data 
  -StartupProject XYZProjectName.Web.Api 
  -Context DBStore

The database should be completely new with proper changes and data (if you use seed data)...

Note that the Add-Migration and Update-DataBase commands can be run from PackageManager Console window within Visual Studio. Or Powershell, or Visual Studio code Termial Window...


Posted on Mar 27 by:

jwp profile

John Peters


Angular, React, Typescript and JavaScript. The wave of now!


markdown guide