entity-framework/ef6/modeling/code-first/migrations/existing-database.md
[!NOTE] EF4.3 Onwards Only - The features, APIs, etc. discussed in this page were introduced in Entity Framework 4.1. If you are using an earlier version, some or all of the information does not apply.
This article covers using Code First Migrations with an existing database, one that wasn’t created by Entity Framework.
[!NOTE] This article assumes you know how to use Code First Migrations in basic scenarios. If you don’t, then you’ll need to read Code First Migrations before continuing.
Your first step will be to create a Code First model that targets your existing database. The Code First to an Existing Database topic provides detailed guidance on how to do this.
[!NOTE] It is important to follow the rest of the steps in this topic before making any changes to your model that would require changes to the database schema. The following steps require the model to be in-sync with the database schema.
The next step is to enable migrations. You can do this by running the Enable-Migrations command in Package Manager Console.
This command will create a folder in your solution called Migrations, and put a single class inside it called Configuration. The Configuration class is where you configure migrations for your application, you can find out more about it in the Code First Migrations topic.
Once migrations have been created and applied to the local database you may also want to apply these changes to other databases. For example, your local database may be a test database and you may ultimately want to also apply the changes to a production database and/or other developers test databases. There are two options for this step and the one you should pick depends whether or not the schema of any other databases is empty or currently matches the schema of the local database.
Code First Migrations uses a snapshot of the model stored in the most recent migration to detect changes to the model (you can find detailed information about this in Code First Migrations in Team Environments). Since we are going to assume that databases already have the schema of the current model, we will generate an empty (no-op) migration that has the current model as a snapshot.
In this scenario we need Migrations to be able to create the entire database from scratch – including the tables that are already present in our local database. We’re going to generate an InitialCreate migration that includes logic to create the existing schema. We’ll then make our existing database look like this migration has already been applied.
There are a few things you need to be aware of when using Migrations against an existing database.
Migrations explicitly specifies names for columns and tables when it scaffolds a migrations. However, there are other database objects that Migrations calculates a default name for when applying the migrations. This includes indexes and foreign key constraints. When targeting an existing schema, these calculated names may not match what actually exists in your database.
Here are some examples of when you need to be aware of this:
If you used ‘Option One: Use existing schema as a starting point’ from Step 3:
If you used ‘Option Two: Use empty database as a starting point’ from Step 3:
Database objects that are not part of your model will not be handled by Migrations. This can include views, stored procedures, permissions, tables that are not part of your model, additional indexes, etc.
Here are some examples of when you need to be aware of this: