Using the DROP TABLE command on a temporary table, as with any table, will delete the table and remove all data. In an SQL server, when you create a temporary table, you need to use the # in front of the name of the table when dropping it, as this indicates the temporary table.
it seems there is a problem in migration process, run add-migration command in "Package Manager Console":
Add-Migration Initial -IgnoreChanges
do some changes, and then update database from "Initial" file:
Update-Database -verbose
Edit: -IgnoreChanges is in EF6 but not in EF Core, here's a workaround: https://stackoverflow.com/a/43687656/495455
Maybe you have changed the namespace in your project!
There is a table in your data base called dbo.__MigrationHistory
. The table has a column called ContextKey
.
The value of this column is based on your namespace
. for example is "DataAccess.Migrations.Configuration
".
When you change the namespace, it causes duplicate table names with different namespaces.
So, after you change namespace in code side, change the namespace in this table in database, too, (for all rows).
For example, if you change the namespace to EFDataAccess
, then you should change the values of ContextKey
column in dbo.__MigrationHistory
to "EFDataAccess.Migrations.Configuration
".
Then in code side, in Tools => Package Manager Console, use the update-database
command.
Another option instead of changing the context value in the database is to hard code the context value in your code to the old namespace value. This is possible by inheriting DbMigrationsConfiguration<YourDbContext>
and in the constructor just assign the old context value to ContextKey
, than inherit from MigrateDatabaseToLatestVersion<YourDbContext, YourDbMigrationConfiguration>
and leave that class empty. The last thing to do is call Database.SetInitializer(new YourDbInitializer());
in your DbContext in a static constructor.
I hope your problem will be fixed.
"There is already an object named 'AboutUs' in the database."
This exception tells you that somebody has added an object named 'AboutUs' to the database already.
AutomaticMigrationsEnabled = true;
can lead to it since data base versions are not controlled by you in this case. In order to avoid unpredictable migrations and make sure that every developer on the team works with the same data base structure I suggest you set AutomaticMigrationsEnabled = false;
.
Automatic migrations and Coded migrations can live alongside if you are very careful and the only one developer on a project.
There is a quote from Automatic Code First Migrations post on Data Developer Center:
Automatic Migrations allows you to use Code First Migrations without having a code file in your project for each change you make. Not all changes can be applied automatically - for example column renames require the use of a code-based migration.
Recommendation for Team Environments
You can intersperse automatic and code-based migrations but this is not recommended in team development scenarios. If you are part of a team of developers that use source control you should either use purely automatic migrations or purely code-based migrations. Given the limitations of automatic migrations we recommend using code-based migrations in team environments.
In my case, my EFMigrationsHistory
table was emptied (somehow) and when trying to run update-database
I would get:
There is already an object named 'AspNetUsers' in the database
After seeing the table had been emptied it made sense that it was trying to rerun the initial migration and trying to recreate the tables.
To fix this problem I added rows into my EFMigrationsHistory
table. 1 row for each migration that I knew the database was up to date with.
A row will have 2 columns: MigrationId
and ProductVersion
MigrationId
is the name of your migration file. Example: 20170628112345_Initial
ProductVersion
is the ef version you're running. You can find this by typing Get-Package
into the Package Manager Console and looking for your ef package.
Hope this is helpful for someone.
In my case I had re-named the assembly that contained the code-first entity framework model. Although the actual schema hadn't changed at all the migrations table called
dbo.__MigrationHistory
contains a list of already performed migrations based on the old assembly name. I updated the old name in the migrations table to match the new and the migration then worked again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With