Hi,
I created a migration with
dnx ef migrations add Initial
dnx ef database update
After that I made some changes to the model and I added a new Migration Second
dnx ef migrations add Second.
Now when I try to update the database I get an error.
I tried:
dnx ef database update
dnx ef database update Second
dnx ef database update
The error says the table is already created. This because the update tried to load the "Initial" migration again.
What can I do?
Cheers, Tim
@timvanderweijde can you provide some detailed steps for us to reproduce this? We are not able to get the same result. If you can provide initial code, the change you make, etc.
Hi,
I started to create a demo project and I noticed a difference (and the reason why I got the error).
I also used another tutorial:
http://mvc.readthedocs.org/en/latest/tutorials/mvc-with-entity-framework.html
In there there a line:
if (context.Database.AsRelational().Exists())
{
But this line doesn't work with beta7 anymore. (the tutorial is behind)
I replaced this with this line:
context.Database.EnsureCreated();
So, before Migration I already created the database by running the application. And I quess if the database doesn;t have the table __MigrationHistory it will try to install the Initial migration script. This will always and up with an error related the table already exists. It would be nice to have a little bit more information of missing a MigrationTable e.g.
And how can I do "if (context.Database.AsRelational().Exists())" with beta 7?
Regards, Tim
Hey @timvanderweijde,
Ok that explains it, EnsureCreated totally bypasses migrations and just creates the schema for you, you can't mix this with migrations. EnsureCreated is designed for testing or rapid prototyping where you are ok with dropping and re-creating the database each time. If you are using migrations and want to have them automatically applied on app start, then you can use context.Database.Migrate() instead.
~Rowan
@rowanmiller
In my scenario I'm working on a multi-tenant app and I have a custom connection string set up in the OnConfiguring method of the DbContext.
I want to call context.Database.Migrate() after the DbContext has been configured.
Where is the right place to achieve that?
Posted separately: #15846.
Most helpful comment
Hey @timvanderweijde,
Ok that explains it,
EnsureCreatedtotally bypasses migrations and just creates the schema for you, you can't mix this with migrations.EnsureCreatedis designed for testing or rapid prototyping where you are ok with dropping and re-creating the database each time. If you are using migrations and want to have them automatically applied on app start, then you can usecontext.Database.Migrate()instead.~Rowan