[英]EntityFramework Core can't update Database “id's not same type”
您好我正在嘗試使用初始遷移更新數據庫,但 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 是“唯一標識符”。 該項目一直盯着 Asp.Net.Core2.2,但我最近嘗試將其更新到 3.0。 EntityFramework 包也更新到 3.0。 也許這是某種錯誤? 感謝幫助:)。
向上方法 =>
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");
}
}
外鍵列類型必須與主鍵類型相同。將照片實體中的ClothId列類型更改為uniqueidentifier。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.