I have an ASP.NET Core 5 project. I have a Product table to which I had to add a column of type int, named ViewCount.
I use add-migration addViewcountToProductTable for this.
But when I run update-database, I get this error:
Failed executing DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Category] (
[Id] bigint NOT NULL IDENTITY,
[Name] nvarchar(max) NULL,
[ParentCategoryId] bigint NULL,
[InsertTime] datetime2 NOT NULL,
[UpdateTime] datetime2 NULL,
[IsRemoved] bit NOT NULL,
[RemovedTime] datetime2 NULL,
CONSTRAINT [PK_Category] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Category_Category_ParentCategoryId] FOREIGN KEY ([ParentCategoryId]) REFERENCES [Category] ([Id]) ON DELETE NO ACTION );Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Category' 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, 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 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:47b1be05-37f7-4be2-9067-498c33979829Error Number:2714,State:6,Class:16
There is already an object named 'Category' in the database.
It is worth noting that my project is working correctly so far; however, when I try to add a new field to the products table, I encounter the error mentioned above.
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using UCI.Persistence.Context;
namespace UCI.Persistence.Migrations
{
[DbContext(typeof(DataBaseContext))]
partial class DataBaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.Has
.HasColumnType("datetime2");
b.Property("IsRemoved")
.HasColumnType("bit");
b.Property("RemovedTime")
.HasColumnType("datetime2");
b.Property("RoleId")
.HasColumnType("bigint");
b.Property("UpdateTime")
.HasColumnType("datetime2");
b.Property("UserId")
.HasColumnType("bigint");
b.HasKey("Id");
b.HasIndex("RoleId");
b.HasIndex("UserId");
b.ToTable("userInRoles");
});
modelBuilder.Entity("UCI.Domain.Entities.Products.Category", b =>
{
b.HasOne("UCI.Domain.Entities.Products.Category", "ParentCategory")
.WithMany("SubCategories")
.HasForeignKey("ParentCategoryId")
.OnDelete(DeleteBehavior.Restrict);
b.Navigation("ParentCategory");
});
modelBuilder.Entity("UCI.Domain.Entities.Products.ProductFeatures", b =>
{
b.HasOne("UCI.Domain.Entities.Products.Products", "Product")
.WithMany("ProductFeatures")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
});
modelBuilder.Entity("UCI.Domain.Entities.Products.ProductImage", b =>
{
b.HasOne("UCI.Domain.Entities.Products.Products", "Product")
.WithMany("ProductImages")
.HasForeignKey("ProductId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Product");
});
modelBuilder.Entity("UCI.Domain.Entities.Products.Products", b =>
{
b.HasOne("UCI.Domain.Entities.Products.Category", "Category")
.WithMany()
.HasForeignKey("CategoryId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Category");
});
modelBuilder.Entity("UCI.Domain.Entities.Users.UserInRole", b =>
{
b.HasOne("UCI.Domain.Entities.Users.Role", "Role")
.WithMany("UserInRoles")
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("UCI.Domain.Entities.Users.User", "User")
.WithMany("UserInRoles")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Role");
b.Navigation("User");
});
modelBuilder.Entity("UCI.Domain.Entities.Products.Category", b =>
{
b.Navigation("SubCategories");
});
modelBuilder.Entity("UCI.Domain.Entities.Products.Products", b =>
{
b.Navigation("ProductFeatures");
b.Navigation("ProductImages");
});
modelBuilder.Entity("UCI.Domain.Entities.Users.Role", b =>
{
b.Navigation("UserInRoles");
});
modelBuilder.Entity("UCI.Domain.Entities.Users.User", b =>
{
b.Navigation("UserInRoles");
});
#pragma warning restore 612, 618
}
}
}