Entity Framework Type Discovery


Example

By default Code First includes in model

  1. Types defined as a DbSet property in context class.
  2. Reference types included in entity types even if they are defined in different assembly.
  3. Derived classes even if only the base class is defined as DbSet property

Here is an example, that we are only adding Company as DbSet<Company> in our context class:

public class Company
{
    public int Id { set; get; }
    public string Name { set; get; }
    public virtual ICollection<Department> Departments { set; get; }
}

public class Department
{
    public int Id { set; get; }
    public string Name { set; get; }
    public virtual ICollection<Person> Staff { set; get; }
}

[Table("Staff")]
public class Person
{
    public int Id { set; get; }
    public string Name { set; get; }
    public decimal Salary { set; get; }
}

public class ProjectManager : Person
{
   public string ProjectManagerProperty { set; get; }
}

public class Developer : Person
{
    public string DeveloperProperty { set; get; }
}

public class Tester : Person
{
    public string TesterProperty { set; get; }
}    

public class ApplicationDbContext : DbContext
{
    public DbSet<Company> Companies { set; get; }
}

We can see that all the classes are included in model

enter image description here