2

Our team works on a website with ASP MVC5 & EF5. there are 3 machines involved in my question:

  1. Developer's machine
  2. Test server
  3. Production server

So a regular work cycle is: the developer adding features & puts them on the test server when there are some approved tested features they goes to the Production server. When there are no model changes - everything works great. But, when there are model changes - sometimes the migration gets wrong. and the DB get so messed up so we need to backit up, clear everything & copy the data from the backup. what we do is:

  1. Add migration on dev-env.
  2. Copy all the source to the test/production (Model & Migration folders).
  3. Update database.

when doing this basic cycle some times the migration failes to update because of table already exists; I guess im doing something wrong in the process.

Another bad thing: we have copy of the development environment on the Test & Production servers - my guess is that we dont need VS on the server in order to update version (we use the VS to run the EF commands from the package manager.

My question is

Is there any best practice/manual regarding dev->test->production process & how to do it right with code first & no visual studio on the servers?

yossico
  • 3,421
  • 5
  • 41
  • 76
  • 1
    Migrations also can be automatically executed on the application start. If you use MigrateToLatest db initializer and have disabled AutomaticMigrations, then during startup if DB is different then model - EF will execute pending migrations, and if they are the same - will continue to working. Or just fail with exception if some migration is missing – Sergey Litvinov Feb 01 '16 at 19:15
  • thanks @SergeyLitvinov Can you explain in more detailed manner how it can be done? do you have any good referance regarding that issue? – yossico Feb 01 '16 at 19:18
  • 1
    I wouldn't run migrations against PROD. You can create scripts that will conditionally test for what has been applied (idempotent). See this http://cpratt.co/migrating-production-database-with-entity-framework-code-first/#at_pco=smlwn-1.0&at_si=54ad5c7b61c48943&at_ab=per-12&at_pos=0&at_tot=1 – Steve Greene Feb 01 '16 at 19:19
  • 1
    Here is the idempotent part. Basically you get a long script that checks if each migration has been run on TEST or PROD and executes the ones that have note and update the __MigrationHistory table. https://msdn.microsoft.com/en-us/data/jj591621.aspx?f=255&MSPPError=-2147217396#idempotent – Steve Greene Feb 01 '16 at 19:21
  • Here's one on working in a team environment: http://stackoverflow.com/questions/13314725/migrations-in-entity-framework-in-a-collaborative-environment – Steve Greene Feb 01 '16 at 19:22
  • 1
    Use migrate.exe to run migrations on your build server without VS: https://msdn.microsoft.com/en-us/data/jj618307.aspx – Steve Greene Feb 01 '16 at 19:27
  • Thanks @SteveGreene, In general, do I need automaticMigration set to enabled or Disabled? – yossico Feb 01 '16 at 19:32
  • See "Avoid automatic migrations" here https://msdn.microsoft.com/en-us/data/dn481501.aspx – Steve Greene Feb 01 '16 at 19:42

0 Answers0