When I tried to execute SQLQuery (generated by Update-Database -Verbose -f -Script locally in Visual Studio) on remote database, I see the following error returned by SQL Server Management Studio:
Msg 2714, Level 16, State 6, Line 1
There is already an object named '__MigrationHistory' in the database.
How to solve this?
Just a question. Are you using a different schema other than dbo?
I think its a bug in EF framework where it doesn't check the schema when it does it checks to see if the __MigrationHistory table exists.
I was able to solve this problem by just creating a Dummy __MigrationHistory table with the dbo schema, this tricked the EF6 generator for "creating the table"
CREATE TABLE [dbo].[__MigrationHistory] (
[MigrationId] [nvarchar](150) NOT NULL,
[ContextKey] [nvarchar](300) NOT NULL,
[Model] [varbinary](max) NOT NULL,
[ProductVersion] [nvarchar](32) NOT NULL,
CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId], [ContextKey])
)
If you want to automate it, you'll have to create a Empty Migration with a dummy class that uses the dbo schema and run that migration first to generate the relevant tables. Then run the scripts for the migration with the different schema and it should work.
__MigrationHistory is an auto-generated table used by EF to track what upgrades/patches it has applied to the database. EF is completely aware of that table and handles it on its own. You should ot create/drop/alter that table. It seems your database already has that table. If EF or your upgrade-script tries to create such table, this is strange. You need to carefully review everything and guess/learn what really has happened, because either EF went wild, or your scripts are prepared in a wrong way.
I've seen this happen when doing "Code First from an Existing Database" where the database being pulled from already has __MigrationHistory table.
It ends up added a POCO class of the type. Remove the class, redo migrations and run again.
You should either change your connection string of your startup project to point to the remote database - it would appear that it is pointing to a database that already has a __MigrationHistory table, or generate a full script using
update-database -script -SourceMigration $InitialDatabase
which will script all migrations into a single file and check migration by migration to see which ones it needs to run. The first thing this script does is check for the existence of __MigrationHistory table and create it if it doesn't exist.
A setup for a migration might be missing. Typing the command add-migration MigrationName in the Package Manager Console before updating the database worked for me, as suggested in this tutorial
Change database name in the connection string in web.config solved to me after drop database. It's a workaround that helps in dev environments. The db was recreated with the new entities.
I opened SQL Explorer from visual studio and connected to the database. Dropped table using SQL directly. Deleted Migrations folder. Ran migrations again. As it is test database, there was no big issue in dropping table. If it has data, you need to think of a different solution. Nothing else worked.
Tools > SQL Server > New Query
drop table "table_name";
Solution Explorer > Right click on Migrations and Delete
Tools > Nuget package manager > Package manager console
In the Package Manager Console (typically opens at bottom of screen): Add-Migration InitialCreate4
Update-Database
If you are using an existing database with EF and .net core then as of writing you will need to create an initial migration (dotnet ef database update) with empty Up/Down methods (manually delete the code) as -IgnoreChanges is not implemented:
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
This will prevent your migration from attempting to create the tables as is normal on a first migration in a code first scenario.
Run
dotnet ef database update
Once complete you will be able to update using normal migrations.
See:
https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/existing-database
And
What is the equivalent of the -IgnoreChanges switch for entity-framework core in CLI?
Go to Migration Folder.
You will see two files one is the configuration and the other file starts with some number like 019763632... open it
comment the code which Generates the already created table. and then enable automatic Migration Enable-Migrations -EnableAutomaticMigrations
The update-database
This worked for me try it
I managed to solve this error by removing the part where I make sure the database is created.
//Database.EnsureCreated();
This part is supposed to create the database if it did not exist only.
Related
I am using Entity framework and code first approach in my project. I have done some Migrations to my server database and it was working fine. now I added a few more tables and try to create a migration by add-migration command to the database on server.
The command runs with no error and created a migration file.
But, when I check the migration file the code in the migration file was to create all tables again to database (create table for already created tables are also in the up method).
When I run Update-database command it returned an error saying
...table already exist in database.
I try it on some local database on my system and it was working fine.
Can anyone help me with this?
Is there any special permission to set in the server?
Is it because some services from visual studio getting blocked? If so how can I solve it?
When trying to run my ASP.NET MVC application locally, everything works fine. But once I've deployed it Azure, it keeps giving me the error:
Unable to update database to match the current model because there are pending changes and automatic migration is disabled.
So I connected to the remote database using the Package Manager Console in Visual Studio and ran the Update-Database command. This gave me the same error. When I look at the database inside the SQL Server Management Studio, it looks like all migrations are applied.
So after this I tried running Add-Migration to see if there were any changes I didn't notice. The migration it created was exactly the same as the previous two migration (there is nothing in there that's not in one of those migrations).
So far the site ran just fine for about two weeks, including a few migrations. I'm at a loss as to how to solve this, so any help is appreciated.
I'm certain I'm connecting to the right database, because I see it change when I update it to a target migration. I'm also targeting the correct project.
If you are sure that the Add-Migration is creating the script for changes that are already applied, you need to update the migration state of your database. You can do this by adding an empty migration. This will capture the state of your current model.
Add-Migration MergeChanges –IgnoreChanges
After running this command you will have an empty migration script. Now you can update your database to match the model state. Since the actual migration doesn’t contain any changes, it will simply add a row to the __MigrationsHistory table indicating that this migration has already been applied.
NOTE Run this only if you do not have any pending changes and yet you see the error message to update the database.
I am developing a project with model first approach using Entity Framework in MVC. After some days I needed to add some properties to my existing model. Later when I tried to build the solution it gave me the following error:
Model has changed.
So, I googled it found one solution:
Database.SetInitializer<ClubmansGuideDB>(new DropCreateDatabaseIfModelChanges<ClubmansGuideDB>());
It's working fine but how does entity framework maintain existing data in particular model associated table? Can someone please explain?
You should not use this Initializes, instead, use DB Migration which will generate a script to only alter the database with the few properties you added to your model.
You can do this:
1- Open NuGet Package Manager Console from Tools Menu->Nu Get Package Manager
2- Run Enable-Migrations command
3- Run Add-Migration Initial command, this will add a migration with code to create the whole database if this is your first time.
4- If you already have a migration, then run add-migration -Name somename
5- Run the command update-database -script this will generate a database script to alter the DB.
Use the script in step #5 and run it against the database and it will only do the needed modifications
I my MVC 5 project I used code first approach for creating the database. However after enable migration, I am getting error
Migrations is enabled for context 'ApplicationDbContext' but the database does not exist or contains no mapped tables. Use Migrations
to create the database and its tables, for example by running the
'Update-Database' command from the Package Manager Console.
However if i delete migration folder then error is resolved but then I an unable to use migration. Please help. I am attaching screen shot for the error.
I run the command for add-migration and then update-database. That did help me. But if i change connection string and run the project again then it is not creating database until migration folder is not deleted.
Before you can use your database, you need to create a migration, that describes structure of your database with the Add-Migration command and then update your database to match you current model with the Update-Database command.
You might find this tutorial useful.
Enable migrations just creates scripts for creating and seeding that database. You need to run update-database from the package manager console to create the database. The database will be created on the server referenced in your web.config connection strings
I have been made a few changes to my database locally and have been adding migrations and updating the database via the Package Manager Console.
Now I've checked in and deployed out to the dev server however I am getting the error:
The model backing the 'MyProjectContext' context has changed since the
database was created. Consider using Code First Migrations to update
the database (http://go.microsoft.com/fwlink/?LinkId=238269).
I know this is telling me that my version of the database is out of date however I don't want to drop the database this time so im trying to generate a script to manually run on the database however I don't fully understand how I can tell it which is the last migration that the dev db is aware of and which is the current, up to date one, ive tried the below however its not working:
So If "Added IsImportant to exceptions" was the last migration updated on the dev server, and "Set ImportantId as Identity Specification" was the last update I manually run locally, how do I generate the right script for dev?
Turns out I was doing it right but needed the full configuration name with double quotes.
Update-Database -Script -SourceMigration:"201507091527309_Added IsImportant to exception" -TargetMigration:"201507281410132_Set ImportantId as Identity Specification"
Once I ran this it generated the script and I was able to upgrade the database.