简体   繁体   English

EntityFramework Core 无法更新数据库“id's not same type”

[英]EntityFramework Core can't update Database “id's not same type”

Hi I'm trying to update database with initial migration, but EFCore is saying that您好我正在尝试使用初始迁移更新数据库,但 EFCore 说

Column 'Clothes.Id' is not the same data type as referencing column 'Photos.ClothId' in foreign key 'FK_Photos_Clothes_ClothId'.
Could not create constraint or index. See previous errors.

It's weird, because even in created migrations it says that id's are "uniqueidentifier".这很奇怪,因为即使在创建的迁移中,它也说 id 是“唯一标识符”。 The project was stared with Asp.Net.Core2.2 but I recently tried to update it to 3.0.该项目一直盯着 Asp.Net.Core2.2,但我最近尝试将其更新到 3.0。 Also EntityFramework packages was udpated to 3.0. EntityFramework 包也更新到 3.0。 Maybe it's some kind of bug?也许这是某种错误? Thanks for help:).感谢帮助:)。

UpMethod =>向上方法 =>

protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Clothes",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWID()"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()"),
                    LastTimeModified = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()"),
                    Name = table.Column<string>(nullable: false),
                    Price = table.Column<decimal>(type: "decimal(6,2)", nullable: false),
                    BoughtOn = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "DATEADD(day, -1, GETDATE())"),
                    ClothType = table.Column<int>(nullable: false),
                    Size = table.Column<string>(nullable: false),
                    Color = table.Column<string>(nullable: false),
                    Manufacturer = table.Column<string>(nullable: false),
                    ClothUrl = table.Column<string>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Clothes", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Photos",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWID()"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()"),
                    LastTimeModified = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()"),
                    PhotoUrl = table.Column<string>(type: "varchar(max)", nullable: false),
                    ClothId = table.Column<string>(type: "varchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Photos", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Photos_Clothes_ClothId",
                        column: x => x.ClothId,
                        principalTable: "Clothes",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Photos_ClothId",
                table: "Photos",
                column: "ClothId",
                unique: true);
        }

I'm using fluentApi to provide settings etc.我正在使用 fluentApi 提供设置等。

 public class WardrobeContext : DbContext
    {
        public WardrobeContext(DbContextOptions<WardrobeContext> options) : base(options)
        {
        }

        public DbSet<Cloth> Clothes { get; set; }
        public DbSet<Photo> Photos { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.ApplyConfiguration(new BaseEntityConfiguration<Cloth>());
            builder.Entity<Cloth>(ConfigureCloth);

            builder.ApplyConfiguration(new BaseEntityConfiguration<Photo>());
            builder.Entity<Photo>(ConfigurePhoto);
        }

        private void ConfigureCloth(EntityTypeBuilder<Cloth> builder)
        {
            builder.Property(cloth => cloth.Name)
                .IsRequired(true);
            builder.Property(cloth => cloth.BoughtOn)
                .HasDefaultValueSql("DATEADD(day, -1, GETDATE())")
                .HasColumnType("datetime2");
            builder.Property(cloth => cloth.ClothType)
                .IsRequired(true);
            builder.Property(cloth => cloth.ClothUrl)
                .IsRequired(true);
            builder.Property(cloth => cloth.Color)
                .IsRequired(true);
            builder.Property(cloth => cloth.Manufacturer)
                .IsRequired(true);
            builder.Property(cloth => cloth.Price)
                .IsRequired(true)
                .HasColumnType("decimal(6,2)");
            builder.Property(cloth => cloth.Size)
                .IsRequired(true);
            builder.HasOne(cloth => cloth.Photo)
                .WithOne(x => x.Cloth)
                .HasForeignKey<Photo>(photo => photo.ClothId);
        }

        private void ConfigurePhoto(EntityTypeBuilder<Photo> builder)
        {
            builder.Property(photo => photo.PhotoUrl)
                .IsRequired(true)
                .HasColumnType("varchar(max)");
            builder.Property(photo => photo.ClothId)
                .IsRequired(true)
                .HasColumnType("varchar(max)");
            builder.HasIndex(ix => ix.ClothId)
                .IsUnique();
        }
    }
internal class BaseEntityConfiguration<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : BaseEntity
    {
        public void Configure(EntityTypeBuilder<TEntity> builder)
        {
            builder.Property(baseEntity => baseEntity.Id)
                .HasDefaultValueSql("NEWID()")
                .HasColumnType("uniqueidentifier");
            builder.Property(baseEntity => baseEntity.CreatedAt)
                .HasDefaultValueSql("GETDATE()")
                .HasColumnType("datetime2")
                .ValueGeneratedOnAdd();
            builder.Property(baseEntity => baseEntity.LastTimeModified)
                .HasDefaultValueSql("GETDATE()")
                .ValueGeneratedOnAdd()
                .HasColumnType("datetime2");
        }
    }

The foreign key column type must be the same as the primary key type.Change the type of ClothId column in the photo entity to uniqueidentifier.外键列类型必须与主键类型相同。将照片实体中的ClothId列类型更改为uniqueidentifier。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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