0

Okay, so this is my situation. I have generated POCOs from a db and developed around them. I have also renamed and changed some of these POCOs to be more readable. I have now made changes to my original db scheme and need to migrate the changes back to my models without overriding my changes except where the scheme has changed. It there a way to generate a *.sql script to hand to by DBAs? If so, it there a way to compare two dbs and generate a change script? ie - dev db => prod db.

4
  • Are you doing code first or database first? There is a schema compare in VS2013. techbubbles.com/sql-server/… Commented Feb 10, 2016 at 18:43
  • It started as db first and now it is mixed. Commented Feb 10, 2016 at 18:44
  • 1
    Yuck. If it's database first just propagate the changes down. The way to prevent overriding is to use partial classes (dzapart.blogspot.com/2011/11/…). If it's code fist you can generate scripts for the DBA. Commented Feb 10, 2016 at 18:51
  • What if I only use code first approach from now on, can I generate this script? Or does it have to start that way? If it is possible can you post it as an answer? Commented Feb 10, 2016 at 19:47

1 Answer 1

1

You can switch a project to code first and generate the scripts you need via migrations. See the link below for a guide on moving from db first to code first, but it sounds like you may be partially there already.

1) enable-migrations for the project with your context if you haven't already.

2) create a baseline migration. EF will use this as a starting point so you won't get a bunch of code to create the objects that already exist. The ignore changes flag tells EF not to create the existing objects. https://msdn.microsoft.com/en-us/data/dn579398.aspx?f=255&MSPPError=-2147217396#option1

create-migration InitialCodeFirst -IgnoreChanges

3) Now modify your schema as you normally would and create a migration:

add-migration SomeNewThing

4) Create a script for a different database (like PROD) by using -Script. This will not update your database it just creates a script, so I usually run it a second time without -Script:

update-database -Script    // creates a script (in VS), but does not apply
update-database            // updates the database your connect string points to

5) DBA runs script and this will add a record to __MigrationHistory to identify it as being applied.

http://devgush.com/2014/02/24/migrating-a-project-from-database-first-to-code-first/

Here is a useful link on deployment: 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

Sign up to request clarification or add additional context in comments.

1 Comment

Outstanding information! Thanks again for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.