1

In my Entity Framework Code First project, if I run the

update-database

command through the Package Manage Console, my Seed() method runs successfully. If, however, I run the command with the -script parameter:

update-database -script

...the Seed() method is not called and the resulting SQL does not contain the SQL commands for seeding the database. This behaviour is repeatable. I'm attempting to create a full DB Script for deployment.

Why is there a discrepancy between the SQL Commands run with -script and without it, and how can I get update-database to run the Seed() method when using the -script switch?

Ryan Shripat
  • 5,574
  • 6
  • 49
  • 77

1 Answers1

1

I would argue that this is not a discrepancy, but a by design feature.

For one, when you use -script, the database update is not performed, so the Seed() method should not be called either. And also there is the fact the migrations is a design time tool, and Seed() is a method that has be compiled and called at runtime.

And as for why isn't the contents of the Seed() method included in the generated script, my answer is also the different notions of runtime and design time. Imagine that you have a Seed() method like this:

protected override void Seed(PersonContext context)
{
   Random r = new Random();
   context.People.Add(new Person("PersonA", r.Next(90));
   context.SaveChanges();
}

Now the Random instance is created at runtime, and the Next() is called at runtime. How would you try to compile that to SQL? What if you have a file and you read data from it and put that into the database? These are all things that cannot be evaluated at design time, only at runtime (i.e. if the file is not at the right place at design time, it's not a problem, only at runtime). And that's why a design time tool like the migration scaffolder cannot evaluate it.

Akos Nagy
  • 4,201
  • 1
  • 20
  • 37
  • Fair enough. Databases, however, usually do require some sort of fairly static 'seed' data to express things like Enums. I can't use update-database -script to generate the DB Deployment script for Ops that doesn't include things like EmployeeType (1, "Permanent") (2, "Temporary") etc etc. – Ryan Shripat Mar 10 '17 at 18:38
  • 1
    Yes, that's true. But that's not in the scope of migrations. If you want something like this, you have to write it on your own. I also follow this approach in most of my projects. If you're worried about how to structure your project or where to put these scripts, you should consider SSDT. But still in that case you have to write these scripts yourself. – Akos Nagy Mar 10 '17 at 18:51
  • Would have been really convenient to do it in Configuration.cs - that's in source control already, and implicitly in lockstep with the migrations. I'd like to automate this as much as possible. I should ask another SO question :) – Ryan Shripat Mar 10 '17 at 19:39
  • One option might be to put the seed data in a Migration's Up() method (http://stackoverflow.com/a/9344260/1943) but there are risks associated with that. – Ryan Shripat Mar 10 '17 at 19:55