1

I've started looking into Entity Framework migrations on 4.3.1. Have a few questions:

  1. What's preferred during development? Why should I not just drop and recreate my database always and then reseed. If I use code first migrations, can I choose to seed my db initially and then add a seed method to each migration to only add in new data? If i use automatic migrations, is it possible to do something similar? i.e. seed initially and then seed as required?

  2. What is the benefit of using migrations during development? I only actually need migrations when moving to production. So, I need to create my initial script and then scripts for each migration, so would it be possible to only use migrations once i want to move to production and at that point create an initial script and maintain a migration history from that point onwards?

1 Answer 1

1

Well, in our case, we started to use Migrations because in our company, devs don't have the necessary rights to create a DB, which lead to the amusing scenario where I dropped the DB a couple of times and had to ask the db admin to recreate it each time...
In my opinion, it seems easier to incrementally grow your DB, rather than having to recreate it each time. If I were to have to drop and recreate our DB every time a property is added, deleted or changed, I'd never see the end of it.

I've not yet seen a possibility for incremental seeds, unless perhaps if you create manual migration files.
Migrations has the possibility to go to a specific version (either forwards or backwards) and it is possible to generate an SQL script from a migration.
So basically, you don't have to create a migration SQL script by hand anymore, you can get Migrations to do it for you.

3
  • Thanks! it seems like a cool feature, i'm just reluctant to use migrations in my dev environment as it runs the seed method with each update so my data is duplicated. I could use AddorUpdate but from what i've read that seems to come with its own issues, which is why it seems to be easiest and safest to drop and recreate. It would be great if we could turn off the seed method or have multiple seed methods being run with each migration. I'm assuming one way to get around this is to add Sql(insert) into my migration files...but then my seed data isn't in one location, bit messy
    – newbie_86
    Aug 22, 2012 at 12:51
  • Yeah, that's indeed annoying. We solved it quick and dirty by first checking if a certain record (which would normally be seeded) was already present in the DB and if so abort the seeding. From what I read in other SO posts, it's perfectly possible to seed incrementally if you use manual migrations. It's possible to turn of seeding: just don't put any code in your seed method :-) Aug 22, 2012 at 13:26
  • You can use AddOrUpdate() method which provides a simple way to do that :)
    – Web Dev
    Sep 15, 2016 at 1:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.