I am using EF6.
I created a migration which had some issues, because SQL had created a index that it knew nothing about. Trying to manually fix up what EF migrations had generated created more problems than it solved, so I decided to delete the entire table in SQL, then re-scaffold the migration.
The problem is that the new migration expects the table to be there. I expected migrations to know that it wasn't there, but this is obviously not how it works? Is that correct?
Is there a way to tell Migrations to look at the current DB and re scaffold from there?
Thanks
Depending on where you are in development, the easiest thing might be to just delete existing migrations and apply a new baseline using the -IgnoreChanges attribute (https://msdn.microsoft.com/en-us/data/dn579398.aspx#option1).
Otherwise, you can go through the generated Up() code and comment out lines that don't need to be applied. Then do Update-Database and your model will be back in sync.
Related
I have a requirement where if a table of a DB gets mistakenly dropped, we need it back, with or without the data. We already use Flyway for migration, is there any way we can achieve this using Flyway or otherwise?
I think you could hack a solution in place using callbacks (SQL or Java) but you've got to ask how can a table get deleted if you are using flyway to control migrations and amendments to your database in the first place.
This is fundamentally what flyway is intended to prevent as the following snippet from the flyway FAQ confirms and the solution may be to close the possibility of external amendments being applied in the first place.
Can I make structure changes to the DB outside of Flyway?
No. One of the prerequisites for being able to rely on the metadata in the database and having reliable migrations is that ALL database changes are made by Flyway. No exceptions. The price for this reliability is discipline. Ad hoc changes have no room here as they will literally sabotage your confidence. Even simple things like adding an index can trip over a migration if it has already been added manually before.
It seems not to be possible with versioned migrations, since they are applied only once, or with repeatable migrations, because they are reapplied only if check sum changes.
Another option - is to create a callback, which will run after migration.
For example, afterMigrate callback could do it, you just need to create a script named afterMigrate.sql in the location, used to load migrations. Now you just need to make a SQL-script to recreate some table if it not exists.
Some vendors support such an options, for example, with PostgreSQL you can use CREATE TABLE query with the IF NOT EXISTS option, to create a table only it doesn't exists.
I am trying to Change the Primary key of a table via Entity Framework Core Migrations:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_Permissions",
table: "Permissions");
}
When I try to update the database I get the following error message:
To change the IDENTITY property of a column, the column needs to be dropped and recreated.
How can I update the database?
I have found a solution:
This seems to be a bug in EF Core 1.1
I changed the Migration file.
More Information about this can be found here: https://thisworksonmymachine.com/2017/02/13/ef-core-the-setup-part-4/
It's always a risky idea to manipulate the migrations...
The simplest way is to clean the slate, if possible:
Take the table out from the DBContext (this will drop the table)
Create Migration and update database
Restore the table in the context and do your changes.
Create Migration and update database.
Obviously, you need to consider backup and restore of data and constraints...
On attempting to get IdentityServer4 EF migrations (SQLServer) I had this same problem. Updating Microsoft.EntityFrameworkCore to v2.1.3 and Microsoft.EntityFrameworkCore.Tools to v2.1.3 seemed to fix the issue.
The following is not a solution for everyone. Namely, if you have an established database in multiple environments.
If you don't care about your migrations, simply start from scratch by deleting all of your migrations. Then recreate the first one with your new schema. Should work just fine.
I am working on jhipster spring using angular js and database as "liquibase".Why We need to delete whole database when we have done change in our db-changelog.xml?.if i have add one field to old table in database then i have a get exception t_user table is already exist.which mean we have to remove t_user table or loss our data.please help and provide any other way to change our database without deleting whole database.
Thanks in advance
Yesterday, we released the version 0.11; which supports the generation of the changelogs containing only your changes. The changes are applied automatically on the database. No need to drop your database now.
Try it. http://jhipster.github.io/2014/02/19/jhipster-release-0.11.0.html
I have not used jhipster at all, but normal liquibase usage is to not keep dropping the database but rather append new changeSets to your db-changelog.xml file. For example, if you originally had a changeSet and need to add a column you append an changeSet. That way you don't lose data and liquibase keeps track of which changes have been ran against your databases.
I'm working on a multi-tenant MVC 4 application and I'm going with the one schema per customer approach. I'd like to run the database migrations in code when a customer signs up. Is this possible using EF 5/Code First Migrations?
So when a customer signs up, I'll create an account in dbo. I'll then check if their subdomain exists as a schema in the database, if not, I'll create the schema and ideally run the migrations.
Thanks!
Clarification
When I create the new schema for the customer in the database, I want to run the migrations for that new schema. So for example,
I'll have schema1.Products and schema2.Products.
If I'm getting it right what you want...
You could use something like this
var migrator = new DbMigrator(new Configuration());
if (migrator.GetPendingMigrations().Any())
migrator.Update();
Or even better you may want to create your own initializer - e.g. check these posts of mine...
What is the correct use of IDatabaseInitializer in EF?
How to create initializer to create and migrate mysql database?
The problem I see with your approach is that'd you'd have to 'separate' the account model/database - and the one that you're trying to migrate. Since you mentioned multi-tenant that may already be the case.
But I guess you could also crete the 'base entities' for accounts etc. - and then migrate the rest on top. That's a bit complex scenario - the model is created once (per connection) on start (first use) and cached from then on. So the only for that would be to restart reload, I think (don't hold my word for it, just thinking out loud here)
Not sure if this is what you're asking for, you can run code migrations from command line:
packages\EntityFramework.5.0.0\tools\migrate.exe Example.dll /startUpDirectory:Example\bin
So in theory you could call this whenever a new customer signed up.
In my code I am trying to check if my entity framework Code First model and Sql Azure database are in sync by using the "mycontext.Database.CompatibleWithModel(true)". However when there is an incompatibility this line falls over with the following exception.
"The model backing the 'MyContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data."
This seems to defeat the purpose of the check as the very check itself is falling over as a result of the incompatibility.
For various reasons I don't want to use the Database.SetInitializer approach.
Any suggestions?
Is this a particular Sql Azure problem?
Thanks
Martin
Please check out the ScottGu blog below:
http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx
Here is what is going on and what to do about it:
When a model is first created, we run a DatabaseInitializer to do things like create the database if it's not there or add seed data. The default DatabaseInitializer tries to compare the database schema needed to use the model with a hash of the schema stored in an EdmMetadata table that is created with a database (when Code First is the one creating the database). Existing databases won’t have the EdmMetadata table and so won’t have the hash…and the implementation today will throw if that table is missing. We'll work on changing this behavior before we ship the fial version since it is the default. Until then, existing databases do not generally need any database initializer so it can be turned off for your context type by calling:
Database.SetInitializer<Production>(null);
Using above code you are no recreating the database instead using the existing one so I don't think using Database.SetInitializer is a concern unless you have some serious thoughts about using it.
More info: Entity Framework Code Only error: the model backing the context has changed since the database was created