包管理器控制台中使用 EF Core 更新数据库时出错

问题描述 投票:0回答:0

我在 ASP.NET Core 5 中使用 EF Core 创建了一个数据库,其中涉及四个表,例如

User
Role
UserinRole
Category
。那工作正常。

现在我想用

Product
命令添加一个包含
Productimage
FeatureProduct
列的
add-migration
表。

当我使用

update-database
时,我得到这个错误:

PM> Update-Database
Build started...
Build succeeded.

System.InvalidOperationException:要更改列的 IDENTITY 属性,需要删除并重新创建该列。

在 Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(AlterColumnOperation 操作,IModel 模型,MigrationCommandListBuilder 生成器)
在 Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__83_4(MigrationsSqlGenerator g,MigrationOperation o,IModel m,MigrationCommandListBuilder b)
在 Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation 操作,IModel 模型,MigrationCommandListBuilder 生成器) 在 Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(MigrationOperation 操作,IModel 模型,MigrationCommandListBuilder 生成器) 在 Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList

1 operations, IModel model, MigrationsSqlGenerationOptions options) at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(IReadOnlyList
1 操作,IModel 模型,MigrationsSqlGenerationOptions 选项) 在 Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(迁移迁移,MigrationsSqlGenerationOptions 选项) 在 Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<>c__DisplayClass16_2.b__2() 在 Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(字符串 targetMigration) 在 Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(字符串 targetMigration、字符串连接字符串、字符串上下文类型) 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(字符串 targetMigration、字符串连接字符串、字符串上下文类型) 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0() 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(动作动作)

要更改列的 IDENTITY 属性,需要删除并重新创建该列。

这是我的代码:

// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using UCI.Persistence.Context;

namespace UCI.Persistence.Migrations
{
    [DbContext(typeof(DataBaseContext))]
    [Migration("20230224134454_product")]
    partial class product
    {
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
            #pragma warning disable 612, 618
            modelBuilder
                .HasAnnotation("Relational:MaxIdentifierLength", 128)
                .HasAnnotation("ProductVersion", "5.0.13")
                .HasAnnotation("SqlServer:ValueGenerationStrategy", 
    SqlServerValueGenerationStrategy.IdentityColumn);

            modelBuilder.Entity("UCI.Domain.Entities.Products.Category", b =>
            {
                b.Property<long>("Id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("bigint")
                    .HasAnnotation("SqlServer:ValueGenerationStrategy", 
SqlServerValueGenerationStrategy.IdentityColumn);

                b.Property<DateTime>("InsertTime")
                    .HasColumnType("datetime2");

                b.Property<bool>("IsRemoved")
                    .HasColumnType("bit");

                b.Property<string>("Name")
                    .HasColumnType("nvarchar(max)");

                b.Property<long?>("ParentCategoryId")
                    .HasColumnType("bigint");

                b.Property<DateTime?>("RemovedTime")
                    .HasColumnType("datetime2");

                b.Property<DateTime?>("UpdateTime")
                    .HasColumnType("datetime2");

                b.HasKey("Id");

                b.HasIndex("ParentCategoryId");

                b.ToTable("Category");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Products.Product", b =>
            {
                b.Property<long>("Id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("bigint")
                    .HasAnnotation("SqlServer:ValueGenerationStrategy", 
 SqlServerValueGenerationStrategy.IdentityColumn);

                b.Property<string>("Brand")
                    .HasColumnType("nvarchar(max)");

                b.Property<long>("CategoryId")
                    .HasColumnType("bigint");

                b.Property<string>("Description")
                    .HasColumnType("nvarchar(max)");

                b.Property<bool>("Displayed")
                    .HasColumnType("bit");

                b.Property<DateTime>("InsertTime")
                    .HasColumnType("datetime2");

                b.Property<int>("Inventory")
                    .HasColumnType("int");

                b.Property<bool>("IsRemoved")
                    .HasColumnType("bit");

                b.Property<string>("Name")
                    .HasColumnType("nvarchar(max)");

                b.Property<int>("Price")
                    .HasColumnType("int");

                b.Property<DateTime?>("RemovedTime")
                    .HasColumnType("datetime2");

                b.Property<DateTime?>("UpdateTime")
                    .HasColumnType("datetime2");

                b.HasKey("Id");

                b.HasIndex("CategoryId");

                b.ToTable("Products");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Products.ProductFeatures", b =>
            {
                b.Property<long>("Id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("bigint")
                    .HasAnnotation("SqlServer:ValueGenerationStrategy", 
SqlServerValueGenerationStrategy.IdentityColumn);

                b.Property<string>("DisplayName")
                    .HasColumnType("nvarchar(max)");

                b.Property<DateTime>("InsertTime")
                    .HasColumnType("datetime2");

                b.Property<bool>("IsRemoved")
                    .HasColumnType("bit");

                b.Property<long>("ProductId")
                    .HasColumnType("bigint");

                b.Property<DateTime?>("RemovedTime")
                    .HasColumnType("datetime2");

                b.Property<DateTime?>("UpdateTime")
                    .HasColumnType("datetime2");

                b.Property<string>("Value")
                    .HasColumnType("nvarchar(max)");

                b.HasKey("Id");

                b.HasIndex("ProductId");

                b.ToTable("ProductFeatures");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Products.ProductImage", b =>
            {
                b.Property<long>("Id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("bigint")
                    .HasAnnotation("SqlServer:ValueGenerationStrategy", 
   SqlServerValueGenerationStrategy.IdentityColumn);

                b.Property<DateTime>("InsertTime")
                    .HasColumnType("datetime2");

                b.Property<bool>("IsRemoved")
                    .HasColumnType("bit");

                b.Property<long>("ProductId")
                    .HasColumnType("bigint");

                b.Property<DateTime?>("RemovedTime")
                    .HasColumnType("datetime2");

                b.Property<string>("Src")
                    .HasColumnType("nvarchar(max)");

                b.Property<DateTime?>("UpdateTime")
                    .HasColumnType("datetime2");

                b.HasKey("Id");

                b.HasIndex("ProductId");

                b.ToTable("ProductImage");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Users.Role", b =>
            {
                b.Property<long>("id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("bigint")
                    .HasAnnotation("SqlServer:ValueGenerationStrategy", 
SqlServerValueGenerationStrategy.IdentityColumn);

                b.Property<string>("name")
                    .HasColumnType("nvarchar(max)");

                b.HasKey("id");

                b.ToTable("Roles");

                b.HasData(
                    new
                    {
                        id = 1L,
                        name = "Admin"
                    },
                    new
                    {
                        id = 2L,
                        name = "Oprator"
                    },
                    new
                    {
                        id = 3L,
                        name = "Customer"
                    });
            });

        modelBuilder.Entity("UCI.Domain.Entities.Users.User", b =>
            {
                b.Property<string>("FullName")
                    .HasColumnType("nvarchar(450)");

                b.Property<string>("Email")
                    .HasColumnType("nvarchar(450)");

                b.Property<long>("Id")
                    .HasColumnType("bigint");

                b.Property<DateTime>("InsertTime")
                    .HasColumnType("datetime2");

                b.Property<bool>("IsActive")
                    .HasColumnType("bit");

                b.Property<bool>("IsRemoved")
                    .HasColumnType("bit");

                b.Property<string>("Password")
                    .HasColumnType("nvarchar(max)");

                b.Property<DateTime?>("RemovedTime")
                    .HasColumnType("datetime2");

                b.Property<DateTime?>("UpdateTime")
                    .HasColumnType("datetime2");

                b.HasKey("FullName");

                b.HasIndex("Email")
                    .IsUnique()
                    .HasFilter("[Email] IS NOT NULL");

                b.ToTable("Users");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Users.UserInRole", b =>
            {
                b.Property<long>("Id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("bigint")
                    .HasAnnotation("SqlServer:ValueGenerationStrategy", 
SqlServerValueGenerationStrategy.IdentityColumn);

                b.Property<long>("RoleId")
                    .HasColumnType("bigint");

                b.Property<string>("UserFullName")
                    .HasColumnType("nvarchar(450)");

                b.Property<long>("UserId")
                    .HasColumnType("bigint");

                b.HasKey("Id");

                b.HasIndex("RoleId");

                b.HasIndex("UserFullName");

                b.ToTable("userInRoles");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Products.Category", b =>
            {
                b.HasOne("UCI.Domain.Entities.Products.Category", "ParentCategory")
                    .WithMany("SubCategories")
                    .HasForeignKey("ParentCategoryId");

                b.Navigation("ParentCategory");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Products.Product", b =>
            {
                b.HasOne("UCI.Domain.Entities.Products.Category", "Category")
                    .WithMany()
                    .HasForeignKey("CategoryId")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();

                b.Navigation("Category");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Products.ProductFeatures", b =>
            {
                b.HasOne("UCI.Domain.Entities.Products.Product", "Product")
                    .WithMany()
                    .HasForeignKey("ProductId")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();

                b.Navigation("Product");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Products.ProductImage", b =>
            {
                b.HasOne("UCI.Domain.Entities.Products.Product", "Product")
                    .WithMany()
                    .HasForeignKey("ProductId")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();

                b.Navigation("Product");
            });

        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("UserFullName");

                b.Navigation("Role");

                b.Navigation("User");
            });

        modelBuilder.Entity("UCI.Domain.Entities.Products.Category", b =>
            {
                b.Navigation("SubCategories");
            });

        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
        }
    }
}

你知道我如何解决这个错误吗?

c# entity-framework sql-server-2012 entity-framework-migrations asp.net-core-5.0
© www.soinside.com 2019 - 2024. All rights reserved.