在添加迁移时,似乎会跳过部分配置。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new CustomerConfig());
}
public class CustomerConfig : IEntityTypeConfiguration<Domain.Customer>
{
public void Configure(EntityTypeBuilder<Domain.Customer> builder)
{
builder.ToTable(nameof(Domain.Customer));
builder.OwnsOne(
o => o.Created,
sa =>
{
sa.Property(p => p.Reason).HasMaxLength(255).IsUnicode(false);
}
);
builder.Property(x => x.TenantId).IsRequired();
builder.Property(x => x.RowVersion).IsRowVersion();
builder.HasKey(x => new { x.TenantId, x.ProviderId, x.ProviderKey });
builder.Property(x => x.ProviderKey).HasMaxLength(50).IsUnicode(false);
builder.Property(x => x.DisplayName).HasMaxLength(255).IsUnicode(false);
}
}
正在生成:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Customer",
columns: table => new
{
TenantId = table.Column<Guid>(nullable: false),
RowVersion = table.Column<byte[]>(rowVersion: true, nullable: true),
Created_SessionId = table.Column<Guid>(nullable: true),
Created_At = table.Column<DateTimeOffset>(nullable: true),
Created_Reason = table.Column<string>(unicode: false, maxLength: 255, nullable: true),
ProviderId = table.Column<Guid>(nullable: false),
ProviderKey = table.Column<string>(nullable: false),
DisplayName = table.Column<string>(unicode: false, maxLength: 255, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Customer", x => new { x.TenantId, x.ProviderId, x.ProviderKey });
});
}
Domain.Customer.ProviderKey配置似乎未在迁移中应用。
可以找到完整的项目https://github.com/CodeThumper/EFCoreMigrationSample