简体   繁体   中英

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

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". The project was stared with Asp.Net.Core2.2 but I recently tried to update it to 3.0. Also EntityFramework packages was udpated to 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.

 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.

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