There is already an object named in the database
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.
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
"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.