我必须通过添加基类来更改我的实体。 以前,我有这个:
public virtual DbSet<EmailRequest> RefundEmailRequests { get; set; }
public virtual DbSet<EmailRequestState> EmailRequestHistory { get; set; }
改为:
public virtual DbSet<Request> Requests { get; set; }
public virtual DbSet<RequestState> RequestHistory { get; set; }
建设者:
private void ConfigureRequest(EntityTypeBuilder<Request> builder)
{
builder.ToTable("Requests");
builder.HasKey(s => s.Id);
builder.HasMany(s => s.History)
.WithOne()
.HasForeignKey("RequestId")
.OnDelete(DeleteBehavior.Cascade);
builder.HasDiscriminator<string>("Type")
.HasValue<EmailRequest>(EmailRequest.Type)
.HasValue<ExternalRequest>(ExternalRequest.Type);
builder.Ignore(e => e.CurrentState);
}
private void ConfigureRequestState(EntityTypeBuilder<RequestState> builder)
{
builder.ToTable("RequestStates");
builder.Property<int>("Id")
.IsRequired();
builder.HasKey("Id");
builder.HasDiscriminator<string>("Type")
.HasValue<Domain.States.Request.Received>(Domain.States.Request.Received.Type)
.HasValue<Domain.States.Request.Processing>(Domain.States.Request.Processing.Type)
.HasValue<Domain.States.Request.Duplicate>(Domain.States.Request.Duplicate.Type)
.HasValue<Domain.States.Request.Rejected>(Domain.States.Request.Rejected.Type)
.HasValue<Domain.States.Request.FailedProcessing>(Domain.States.Request.FailedProcessing.Type)
.HasValue<Domain.States.Request.Processed>(Domain.States.Request.Processed.Type);
}
EmailRequest
现在继承自Request
(国家原则相同)
添加迁移时,我注意到EF检测到RequestState
的重命名,但没有检测到请求。
migrationBuilder.DropTable(
name: "EmailRequests",
schema: "refunds");
migrationBuilder.RenameTable(
name: "EmailRequestStates",
schema: "refunds",
newName: "RequestStates");
怎么样?我该怎么做来检测重命名?
答案 0 :(得分:-1)
EmailRequest
现在继承自Request
您无法将表重命名为现有表。您需要复制已删除数据中的数据:
migrationBuilder.Sql(
@"
INSERT INTO Requests
SELECT * FROM refunds.EmailRequests;
");
migrationBuilder.DropTable(
name: "EmailRequests",
schema: "refunds");