简体   繁体   中英

There is already an object named 'Account' in the database error in asp .net core

I am trying to migrate the changes in models using following code

Add-Migration "InitialCreate"

Migration files are created after executing this command.

在此处输入图像描述

I tried to execute following command.

Update-Database

But, I am getting this error:

PM> Update-Database
Build started...
Build succeeded.
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (24ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [Account] (
          [id] int NOT NULL IDENTITY,
          [payable_amount] varchar(15) NULL,
          [balance_amount] varchar(15) NULL,
          [created_at] datetime NOT NULL,
          [updated_at] datetime NOT NULL,
          [deleted_at] datetime NULL,
          CONSTRAINT [PK_Account] PRIMARY KEY ([id])
      );
Failed executing DbCommand (24ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Account] (
    [id] int NOT NULL IDENTITY,
    [payable_amount] varchar(15) NULL,
    [balance_amount] varchar(15) NULL,
    [created_at] datetime NOT NULL,
    [updated_at] datetime NOT NULL,
    [deleted_at] datetime NULL,
    CONSTRAINT [PK_Account] PRIMARY KEY ([id])
);
Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Account' in the database.
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 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(TaskCompletionSource`1 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, IReadOnlyDictionary`2 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 connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:1807d8da-31e6-4318-889f-77fff0e3ac25
Error Number:2714,State:6,Class:16
There is already an object named 'Account' in the database.

I have followed given link but it is not helpful.

Link: Update-Database command is not working in ASP.Net Core / Entity Framework Core because object in database already exists

There is already an object named in the database

FYI: I have undo all changes in data model and tried above process but still same error is occurring.

(The question is old, but the answer may help others.)

The error is occurring because you have a database and are trying to add a table there that already exists.

Because you are in the early stages of development, the simple solution would be to delete your database and call update-database again to re-scaffold the database using the last migration.

Also, based on what I'm reading in the comments, you may have started with the EF 'database first' approach and then switched to 'code first'. This is not advisable as it leaves EF in a state of confusion. If this is the case, you will need to backtrack, pick one of these two approaches, and stick with it for the lifetime of your model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM