EF Core:检测表重命名

时间:2018-03-22 13:40:24

标签: c# .net-core ef-core-2.0

我必须通过添加基类来更改我的实体。 以前,我有这个:

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");

怎么样?我该怎么做来检测重命名?

1 个答案:

答案 0 :(得分:-1)

  

EmailRequest现在继承自Request

您无法将表重命名为现有表。您需要复制已删除数据中的数据:

migrationBuilder.Sql(
@"
    INSERT INTO Requests
    SELECT * FROM refunds.EmailRequests;
");

migrationBuilder.DropTable(
    name: "EmailRequests",
    schema: "refunds");