1

I've inherited a project that uses Entity Framework. I've used EF a lot on other projects, but have never come across this issues before.

In a nutshell: adding explicit migrations seems to have broken an initial automatic migration.

Here's a timeline:

  1. I pull the project from source control and run it. I get database created with the expected schema. So far, so normal.
  2. I then add a couple of explicit code-first migrations.
  3. I disable automatic migrations in the configuration.
  4. I push my changes to source control.
  5. Later on, a colleague pulls the code.
  6. He runs it. A database is created, but only contains the schema described in my explicit migrations (which happens to be a single table). The rest of the schema is missing.

What's going on here? How did I manage to break the behaviour that created the initial schema correctly for me in the first place? Incidentally, I also get only the explicit migrations applied when I delete the database and Update-Database again.

Is there some way I can retrospectively convert the initial automatic migration to an explicit migration?

I could always share the schema with my colleague as a creation script, but this doesn't seem to address the underlying problem, which may rear its head when we deploy to other environments.

Tom Wright
  • 11,278
  • 15
  • 74
  • 148
  • I do not really understand your problem. I'm just creating migrations with Add-Migration "NameOfMigration" and call .ServiceProvider.GetRequiredService().Database.Migrate(); on startup. I and my colleague's got the latest schema. I haven't disabled something. – user743414 Aug 31 '17 at 11:45
  • @user743414 I'm glad it's working for you. In my case the initial schema is not described in an explicit migration, which is (I suspect) the root of the issue. – Tom Wright Aug 31 '17 at 11:47
  • How do you create your database the first time? – user743414 Aug 31 '17 at 11:52
  • @user743414 - On my machine? It was a while ago now, so my memory might be rusty. It would either have been by simply building and running the project or by using `Update-Database`. I can reliably say I didn't acquire the schema outside of EF. – Tom Wright Aug 31 '17 at 11:53
  • So when the database is created when your app runs, how do you create it? Which EF method is executed to create/update the database? What happens when your colleague pulls the code and executes "Update-Database –Verbose" before he runs the app? – user743414 Aug 31 '17 at 12:10
  • @user743414 - EF automatically builds the DB if it doesn't already exist. If we run `Update-Database -Verbose` we get the SQL generated by EF to apply the two explicit migrations, but nothing about the initial migration. – Tom Wright Aug 31 '17 at 12:13
  • Automatic migrations in a team environment can get messy. See [here](https://msdn.microsoft.com/en-us/library/dn481501(v=vs.113).aspx). If you generate a script `Update-Database -Script -SourceMigration: $InitialDatabase` it should contain all the objects. Another option if you just want to create a DB from scratch in DEV is to use a different [initializer](http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx). – Steve Greene Aug 31 '17 at 14:29

0 Answers0