1

I'm trying to implement Temporal Normal Form using Entity Framework Core.

Consider having following entity:

public class Employee
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public DateTimeOffset Created { get; set; }

    public DateTimeOffset Deleted { get; set; }

    public DateTimeOffset Effective { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    // other properties
}

Next step would be to split static and temporally-sensitive properties into two tables. Static properties, as name suggests, are properties that would never change once they are inserted (except Deleted property which soft-deletes entity, but let's not focus on that). On the other hand, temporally-sensitive properties are properties which may and probably will change over time. Every change is represented as a record in *Versions table.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Employee>()
        .Map(m =>
            {
                m.Properties(e => new { e.Id, e.Created, e.Deleted, e.FirstName }); // static properties
                m.HasKey(e => e.Id);
                m.ToTable("Employees");
            })
        .Map(m =>
            {
                m.Properties(e => new { e.Id, e.Effective, e.LastName }); // temporally-sensitive properties
                m.HasKey(e => new { e.Id, e.Effective });
                m.ToTable("EmployeeVersions");
            });
}

I've noticed that Entity Framework has this ability to split entities into multiple tables using Map method, but I can't find anything similar in EF Core.

Is it possible (and how) to split entities into multiple table using EF Core?

Stefan Golubović
  • 1,225
  • 1
  • 16
  • 21
  • You should stop trying to coerce your entities into something they aren't. One entity per table works fine, and when you need to merge things together, map them to intermediate models for use elsewhere in your code. – DavidG Jan 19 '22 at 10:03
  • It looks like I have bad news for you... There is an open issue on [github](https://github.com/dotnet/efcore/issues/620) about this. The implementation is planned for [EFCore 7](https://github.com/dotnet/efcore/issues/27185) (November 2022). – RedFox Jan 19 '22 at 10:10
  • Also, EF-core 6 has [support for temporal tables](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/whatsnew#sql-server-temporal-tables). – Gert Arnold Jan 19 '22 at 10:15
  • @RedFox thanks for the comment, that is unfortunate... – Stefan Golubović Jan 19 '22 at 11:10
  • @GertArnold thanks for the comment, I'll check this feature, but I have to use PostgreSQL. – Stefan Golubović Jan 19 '22 at 11:10
  • https://github.com/arkhipov/temporal_tables ? – Caius Jard Jan 19 '22 at 11:30

0 Answers0