Thomas.Benz
Thomas.Benz

Reputation: 8599

Is code first migration automatically run on the server?

I am very familiar with Entity Framework using Database First approach and how to deploy the solution with it.

Basically, in my solution with database first approach, I have a web client project that consumes data access library project that is coded with database first approach.

So, first, I write some SQL Server scripts to add new tables (or make schema changes).

Next, go to the data access library project, using EF edmx designer to update .net from existing database, compile this data access layer, and the DDL reference is automatically updated in the client web project.

When I deploy the solution to the production server:

  1. First, I need to run the t-SQL scripts on the production SQL server
  2. Next, I deploy the 2 updated DDLs (one for the web and 1 for the data access layer) on the web server.

Now, I have a new application that includes a web project and a data access layer project that uses EF Code First approach. I am new to EF code first approach. I know any time when I change the database schema, for instance adding a new table, I need to run code first migration in the Package Management Console in Visual Studio to let my back-end database instance change/update.

My question:

When I deploy the application to the production, what are the steps I should follow?

How is the production SQL server updated that is created with EF Code First approach?

Is it a automatic process or I have to run the migration manually like I do inside Visual Studio under the Package Management Console?

Thanks.

Upvotes: 3

Views: 2341

Answers (2)

Sampath
Sampath

Reputation: 65978

If you're using Azure then you can configure it has automatic process as shown below.

enter image description here

Else you have to do it manually like this :

You have to create a db script and after that you can run it on your production db.

PM> Update-Database -Script

You can refer this doc : Getting a SQL Script

Another option where I normaley use :

When I need to run migrations aginst the production db,I change my conn string to reflect production db and after that run :

PM> Update-Database

Upvotes: 6

BradleyDotNET
BradleyDotNET

Reputation: 61379

You have several options with migrations.

  1. You can generate a script using Update-Database -Script (as @Sampath notes)
  2. You can run Update-Database -ConnectionString="YourDbString" and it will do it against the production database for you
  3. You can use a migration initializer and on app startup it will use the applications connection string to run the migration. Do this by putting a line similar to this in your initialization routine:

    Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
    

Where Context is your DbContext type and Configuration is the configuration class generated when you made the first migration.

See this MSDN article for more information: https://msdn.microsoft.com/en-us/data/jj591621.aspx

Upvotes: 4

Related Questions