Привет! Я пытаюсь обновить базу данных с начальной миграцией, но 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.
Это странно, потому что даже в созданных миграциях говорится, что идентификаторы являются «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");
}
}