EntityFramework Core无法更新数据库“ ID的类型不相同”

时间:2019-09-29 12:30:54

标签: c# asp.net-core entity-framework-core

嗨,我正在尝试通过初始迁移来更新数据库,但是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.

这很奇怪,因为即使在创建的迁移中,它也指出id是“ uniqueidentifier”。 该项目最初使用Asp.Net.Core2.2,但我最近尝试将其更新为3.0。 EntityFramework软件包也被升级为3.0。也许是某种错误?感谢您的帮助:)。

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

我正在使用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");
        }
    }

1 个答案:

答案 0 :(得分:1)

外键列类型必须与主键相同 类型。将照片实体中的ClothId列的类型更改为uniqueidentifier。