J.Memisevic
J.Memisevic

Reputation: 1454

Entity framework core override property issue

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

Upvotes: 0

Views: 1097

Answers (1)

Lucas dos Santos
Lucas dos Santos

Reputation: 26

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

Upvotes: 1

Related Questions