Open
Description
openedon Jan 31, 2022
Given a temporal table as
CREATE TABLE Thingie
(
ThingieId INT identity NOT NULL PRIMARY KEY CLUSTERED,
PeriodStart date,
PeriodEnd date,
Whatever date,
validFrom DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
validTo DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME (validFrom, validTo)
)
WITH (SYSTEM_VERSIONING = ON);
with context
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace PeriodEndIssue;
internal class Context : DbContext
{
public virtual DbSet<Thingie> Things { get; set; }
public Context(DbContextOptions<Context> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Thingie>(entity =>
{
entity.HasKey(e => e.ThingieId);
entity.ToTable("Thingie", b => b.IsTemporal(t =>
{
t.HasPeriodStart("ValidFrom");
t.HasPeriodEnd("ValidTo");
t.UseHistoryTable("MSSQL_TemporalHistoryFor_1127675065");
}));
});
}
protected override void ConfigureConventions(ModelConfigurationBuilder builder)
{
builder.Properties<DateOnly>()
.HaveConversion<DateOnlyConverter>()
.HaveColumnType("date");
}
}
public class DateOnlyConverter : ValueConverter<DateOnly, DateTime>
{
public DateOnlyConverter() : base(
d => d.ToDateTime(TimeOnly.MinValue),
d => DateOnly.FromDateTime(d))
{ }
}
When calling
b.IsTemporal(t =>
{
t.HasPeriodStart("ValidFrom");
t.HasPeriodEnd("ValidTo");
t.UseHistoryTable("MSSQL_TemporalHistoryFor_1127675065");
}));
I get an exception saying:
System.InvalidOperationException: 'The property 'PeriodStart' cannot be added to type 'Thingie' because the type of the corresponding CLR property or field 'DateOnly' does not match the specified type 'DateTime'.'
If i change the type of PeriodEnd, PeriodStart to DateTime - I do not get that exception for propterty "Whatever".
Thingie class:
namespace PeriodEndIssue;
public class Thingie
{
public int ThingieId { get; set; }
//public DateTime PeriodEnd { get; set; }
//public DateTime PeriodStart { get; set; }
public DateOnly PeriodEnd { get; set; }
public DateOnly PeriodStart { get; set; }
public DateOnly Whatever { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment