I've created a table named USERGOLD
using Add-Migration
and update-database
and it works fine the table is created in SQL Server, but now I'm creating another table FoodMenu
.
I've created a model class and dbset
, I used add-migration
and the table is created in the migration, but when I run update-database
to create the table in SQL Server, it's not working. I get an error
There is already an object named 'UserGold' in the database
I don't know why the update-database
are taking my previous migrations.
Contexto:
public class RestoContext : IdentityDbContext<RestoUser>
{
public RestoContext(DbContextOptions<RestoContext> options)
: base(options)
{
}
public DbSet<Menu> Menu { get; set; }
// public DbSet<Employees> Employee { get; set; }
public DbSet<Employees> Emp { get; set; }
public DbSet<Manager> Manager { get; set; }
public DbSet<GoldMenu> GoldMenu { get; set; }
public DbSet<UserGold> UserGold { get; set; }
public DbSet<FoodMenu> FoodMenu { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
My migration:
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace Resto.Migrations
{
public partial class AddusergoldToDb : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "UserGold",
columns: table => new
{
userId = table.Column<string>(nullable: false),
Usermail = table.Column<string>(nullable: true),
CardNumber = table.Column<string>(nullable: true),
Validedate = table.Column<DateTime>(nullable: false),
Amount = table.Column<float>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_UserGold", x => x.userId);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "UserGold");
}
}
}
Console app:
PM> add-migration AddFoodMenuToDatabase
Build started...
Build succeeded.
To undo this action, use Remove-Migration.
PM> update-database
Build started...
Build succeeded.
> fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
> Failed executing DbCommand (45ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [UserGold] (
[userId] nvarchar(450) NOT NULL,
[Usermail] nvarchar(max) NULL,
[CardNumber] nvarchar(max) NULL,
[Validedate] datetime2 NOT NULL,
[Amount] real NOT NULL,
CONSTRAINT [PK_UserGold] PRIMARY KEY ([userId])
);
> Failed executing DbCommand (45ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [UserGold] (
[userId] nvarchar(450) NOT NULL,
[Usermail] nvarchar(max) NULL,
[CardNumber] nvarchar(max) NULL,
[Validedate] datetime2 NOT NULL,
[Amount] real NOT NULL,
CONSTRAINT [PK_UserGold] PRIMARY KEY ([userId])
);
> Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'UserGold' 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 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:64024c74-897d-421e-bb48-ef96cb315f44
Error Number:2714,State:6,Class:16
There is already an object named 'UserGold' in the database