Efcore: Scaffolding - There is already an object named 'entity_name' in the database.

Created on 18 Apr 2018  路  9Comments  路  Source: dotnet/efcore

Hi,

I read though the documentation in https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/ and created a code first initial migration using PowerShell command (Add-Migration). This step generated three files -

  • TimeStamp_Initial.cs
  • TimeStamp_Initial.Designer.cs
  • DbContextModelSnapshot.cs

I was able to successfully apply the migration to the database. Then I added a new entity and tried generating another migration. This step updated the DbContextModelSnapshot.cs file and produced two new files -

  • TimeStamp_Update.cs
  • TimeStamp_Update.Designer.cs

I then tried to apply the migrations but received an error. When I looked at the Up method in TimeStamp_Update.cs, I see the newly added table along with existing (all) tables that we created in the initial migration. Is this the expected behavior? Should I be manually removing the tables that are already existing in the database from the Up method? May be I'm missing something, kindly guide...

Exception message: There is already an object named 'entity_name' in the database.

Further technical details

EF Core version: 2.0.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.6.6
closed-question

Most helpful comment

I'm having the same problem. How do you solve it?

All 9 comments

I figured out the issue... Closing :)

I'm having the same problem. How do you solve it?

i had same problem and after three hour struggling i find out what's going on

in my case when i wanted to migrate for the first time in up() method the default code wants to create the tables that already existed so i got same error as you

to solve it , just delete dose code and write want you want . for example i wanted to add a column so i just write

migrationBuilder.AddColumn( name: "fieldName", table: "tableName", nullable: true);

Hello i am facing the same issue.I am issuing commands using the .NET-CLI and i keep getting the error "The table [some table] already exists".
I am running :

dotnet ef migrations add Initial //works
dotnet ef  database update Initial

Error

CREATE TABLE [Files] (
[Id] int NOT NULL IDENTITY,
[Reflink] bit NOT NULL,
);
Disposing transaction.
Closing connection to database 'sxsdb' on server 'SomeServer\ADRIANDB'.
Closed connection to database 'sxsdb' on server 'SomeServer\ADRIANDB'.
'SXSContext' disposed.
Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Files' in the database.
at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName) at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:84171560-cf30-4549-a343-772de4f70c51
Error Number:2714,State:6,Class:16
There is already an object named 'Files' in the database.

@sanzor This is SQL Server telling you that the Files table already exists in the database.

i have the same problem i add a new field to table and add new migration just fine but as soon as i run update i get error "There is already an object named 'tableName' in the database".

Everytime i add a new migration with some change the Up() function brings the whole dbcontext all over again. I am not sure why spent 3 days on this.

How can i solve this. I need to work with existsting database. I dont want to drop any table. I scaffold db to entities. and when trying to database update it says There is already an object named

Any news on this? Is still not working as expected.

@ruwentos This is a closed issue. If you're hitting an issue, then please open a _new issue_ and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

Was this page helpful?
0 / 5 - 0 ratings