Entity framework core 为什么初始实体框架核心添加迁移会跳过配置?
添加迁移时,似乎会跳过配置的一部分Entity framework core 为什么初始实体框架核心添加迁移会跳过配置?,entity-framework-core,Entity Framework Core,添加迁移时,似乎会跳过配置的一部分 protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfiguration(new CustomerConfig()); } public class CustomerConfig : IEntityTypeConfiguration<Domain.Customer> { p
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);
}
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
ApplyConfiguration(newcustomerconfig());
}
公共类CustomerConfig:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
builder.ToTable(name of(Domain.Customer));
建筑商(
o=>o.已创建,
sa=>
{
属性(p=>p.Reason).HasMaxLength(255).IsUnicode(false);
}
);
属性(x=>x.TenantId.IsRequired();
属性(x=>x.RowVersion).IsRowVersion();
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 });
});
}
protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.CreateTable(
名称:“客户”,
列:表=>new
{
TenantId=table.Column(可空:false),
RowVersion=table.Column(RowVersion:true,nullable:true),
Created_SessionId=table.Column(可空:true),
已创建_At=table.Column(可空:true),
Created_Reason=table.Column(unicode:false,maxLength:255,nullable:true),
ProviderId=表.列(可空:false),
ProviderKey=table.Column(可空:false),
DisplayName=table.Column(unicode:false,maxLength:255,nullable:true)
},
约束:表=>
{
表.PrimaryKey(“PK_客户”,x=>new{x.TenantId,x.ProviderId,x.ProviderKey});
});
}
迁移中似乎未应用Domain.Customer.ProviderKey配置
完整项目可以在域.Customer.ProviderKey中找到,它是一个字符串属性,是
Up
方法的一部分。我不太明白它的“配置似乎没有被应用”。ProviderKey=table.Column(null:false)-没有maxLength和unicode:false.builder.Property(x=>x.ProviderKey)。HasMaxLength(50)。IsUnicode(false);不等于ProviderKey=table.Column(可为Null:false)