EF核心(用于Oracle)将两个不同的表Map到一个实体

yb3bgrhw  于 2023-05-16  发布在  Oracle
关注(0)|答案(1)|浏览(167)

我们正在处理一个非常旧的oracle数据库,不可能修改,无论如何,有两个表应该被收集到一个,因为我们不能这样做,我们想在API中用ef核心来做,所以创建一个Map,将两个实体(具有不同的属性和不同的PK)合并到一个实体,我们怎么可能做到这一点呢?
第一张表:

public class RegistrationMapper : IEntityTypeConfiguration<Registration>
{
    public void Configure(EntityTypeBuilder<Registration> registration)
    {
        registration.ToTable("PUNTERING", "PUNT");

        registration.HasIndex(e => new { e.ProjectId, e.TaskId }, "IX_PUNTERING");

        registration.HasIndex(e => e.DayRegistrationId, "IX_PUNTERING_DAGID");

        registration.Property(e => e.RegistrationId)
            .HasPrecision(8)
            .ValueGeneratedOnAdd()
            .UseHiLo("SEQ_PUNTERING", "PUNT")
            .HasColumnName("PUNTERINGID");

        registration.Property(e => e.AutoCalculate)
            .HasMaxLength(1)
            .IsUnicode(false)
            .HasColumnName("AUTOCALCULATE")
            .HasDefaultValueSql("'J' ");

        registration.Property(e => e.ProjectId)
            .HasMaxLength(20)
            .IsUnicode(false)
            .HasColumnName("AXPROJECTNR");

        registration.Property(e => e.Comment)
            .HasMaxLength(500)
            .IsUnicode(false)
            .HasColumnName("COMMENTAAR");

        registration.Property(e => e.CreationDate)
            .HasColumnType("DATE")
            .HasColumnName("DATUM_CREATIE");

        registration.Property(e => e.ModificationDate)
            .HasColumnType("DATE")
            .HasColumnName("DATUM_UPDATE");

        registration.Property(e => e.Billable)
            .HasMaxLength(1)
            .IsUnicode(false)
            .HasColumnName("FACTUREREN");

        registration.Property(e => e.CreationHumanResourceId)
            .HasMaxLength(5)
            .IsUnicode(false)
            .HasColumnName("PERSNR_CREATIE");

        registration.Property(e => e.ModificationHumanResourceId)
            .HasMaxLength(5)
            .IsUnicode(false)
            .HasColumnName("PERSNR_UPDATE");

        registration.Property(e => e.ProjectId)
            .HasMaxLength(20)
            .IsUnicode(false)
            .HasColumnName("AXPROJECTNR");

        registration.Property(e => e.DayRegistrationId)
            .HasPrecision(7)
            .HasColumnName("PUNTERING_DAGID");

        registration.Property(e => e.TaskId)
            .HasPrecision(4)
            .HasColumnName("SUBPROJECTID");

        registration.Property(e => e.Transport)
            .HasMaxLength(1)
            .IsUnicode(false)
            .HasColumnName("VERPLAATSING");

        registration.HasOne(d => d.Project)
            .WithMany(p => p.Registrations)
            .HasForeignKey(d => d.ProjectId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_PUNTERING_AXPROJECT");

        registration.HasOne(d => d.DayRegistration)
            .WithMany(p => p.Registrations)
            .HasForeignKey(d => d.DayRegistrationId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_PUNTERING_PUNTERINGDAG");

        registration.HasOne(d => d.Task)
            .WithMany(p => p.Registrations)
            .HasForeignKey(d => new { d.ProjectId, d.TaskId })
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_PUNTERING_SUBPROJECT");
    }
}

第二个:

public class DetailedRegistrationMapper : IEntityTypeConfiguration<DetailedRegistration>
{
    public void Configure(EntityTypeBuilder<DetailedRegistration> detailedRegistration)
    {
        detailedRegistration.ToTable("DEELPUNTERING", "PUNT");

        detailedRegistration.HasIndex(e => e.RegistrationId, "IX_DEELPUNTERING");

        detailedRegistration.Property(e => e.DetailedRegistrationId)
            .HasPrecision(8)
            .ValueGeneratedOnAdd()
            .UseHiLo("SEQ_DEELPUNTERING", "PUNT")
            .HasColumnName("DEELPUNTERINGID");

        detailedRegistration.Property(e => e.Percentage)
            .HasColumnType("NUMBER(6,2)")
            .HasConversion(v => WritePercentageType(v), v => ReadPercentageType(v!))
            .HasColumnName("PERCENTAGE_FACTURATIE");

        detailedRegistration.Property(e => e.RegistrationId)
            .HasPrecision(8)
            .HasColumnName("PUNTERINGID");

        detailedRegistration.Property(e => e.Rest)
            .HasMaxLength(1)
            .IsUnicode(false)
            .HasColumnName("REST");

        detailedRegistration.Property(e => e.EndDate)
            .HasColumnType("DATE")
            .HasColumnName("TIJD_TOT");

        detailedRegistration.Property(e => e.FromDate)
            .HasColumnType("DATE")
            .HasColumnName("TIJD_VAN");

        detailedRegistration.HasOne(d => d.Registration)
            .WithMany(p => p.DetailedRegistrations)
            .HasForeignKey(d => d.RegistrationId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_DEELPUNT_PUNT");
    }
}

新实体

public partial class TimeRegistration {
    public int TimeRegistrationId { get; set; }
    public string HumanResourceId { get; set; } = null!;
    public string ProjectId { get; set; } = null!;
    public int TaskId { get; set; }
    public DateTime? StartDateTime { get; set; }
    public DateTime? EndDateTime { get; set; }
    public PercentageType Percentage { get; set; }
    public string? Comment { get; set; }
    public bool? Billable { get; set; }
    public string? CreationHumanResourceId { get; set; }     
    public double? Duration { get; set; }
}

我可以将每个表作为自己的表,然后对新实体进行选择,但它在代码中看起来很混乱,我还希望能够删除域模型的这两个表,只保留新实体。
有什么想法吗

1mrurvl1

1mrurvl11#

只需编写一个返回实体属性的SQL查询,并配置实体以运行查询。
例如

modelBuilder.Entity<TimeRegistration>().ToSqlQuery("select TimeRegistrationId, HumanResourceId ... from Registration r join DetailedRegistration rd on  ...");

相关问题