K7Buoy
K7Buoy

Reputation: 946

EF Core inherited properties not being reflected in migration script

I am trying to migrate the below models and the inheritance is not being reflected in the migration script generated. What am I missing? I am currently using PM to handle the migration script generation with a simple Add-Migration followed by Update-Database in VS2017 targeting SQL 2016.

public class Facility
    {
        [Key]
        public int ID { get; set; }
        public bool Deleted { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public byte? Image { get; set; }

        public List<LocationFacility> LocationFacilities { get; set; }
    }

    public class Helipad : Facility
    {
        public decimal Size { get; set; }
        public decimal MaximumWeight {get; set;}
    }

Upvotes: 2

Views: 1163

Answers (1)

Smit
Smit

Reputation: 2459

Based on Documentation of EF Core,

In order to have certain type to be included in the model by convention, either you need DbSet property or Navigation property pointing to the type or refer it in OnModelCreating. There is navigation property on Facility class. Perhaps some inverse navigation pointing to Facility on a type which was included in the model caused Facility to be included. But Helipad was not added unless you explicitly refer to it. EF Core sets up TPH for types which are included in the model & have hierarchy. But it does not try to find types in hierarchy if not included.

To fix the issue, the easiest way is to add DbSet property for the type you want to include in your Derived class of DbContext. If you don't want to expose a DbSet property for the type then you can write following in your OnModelCreating method.

modelBuilder.Entity<Helipad>();

Hope this solves the issue.

Upvotes: 2

Related Questions