Entity framework core override property issue

909 Views Asked by At

I have 2 classes

public class Offering
{
  [Required]
  [JsonPropertyName("startDate")]
  public virtual DateTime StartDate { get; set; }

  [Required]
  [JsonPropertyName("endDate")]
  public virtual DateTime EndDate { get; set; }
}
public class ComponentOffering : Offering
{
 [Required]
 [JsonPropertyName("startDateTime")]
 public override DateTime StartDate { get; set; }

 [Required]
 [JsonPropertyName("endDateTime")]
 public override DateTime EndDate { get; set; }
}

In EF Core Table-per-hierarchy when I add values to properties StartDate and EndDate in ComponentOffering model and save it to database I get default DateTime values saved.

Any ideas ?

NOTE : Mappings

modelBuilder.Entity<ComponentOffering>()
                .Property(c => c.StartDate)
                .HasColumnName("StartDate");
modelBuilder.Entity<ComponentOffering>()
                .Property(c => c.EndDate)
                .HasColumnName("EndDate");
1

There are 1 best solutions below

0
Lucas dos Santos On BEST ANSWER

You need ignore abstract class mapping. After that you can map your concrete class property normally.

Using Fluent API it would look something like this:

public void Configure(EntityTypeBuilder<Offering> builder)
{
    builder.ToTable("Offerings");

    builder.Ignore(x => x.StartDate);
    builder.Ignore(x => x.EndDate);
}


public void Configure(EntityTypeBuilder<ComponentOffering> builder)
{
    builder.ToTable("Offerings");

    builder.Property(x => x.StartDate).IsRequired(true);
    builder.Property(x => x.EndDate).IsRequired(true);
}