Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7885353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:02:45+00:00 2026-06-03T05:02:45+00:00

I have the following types: public abstract class Vehicle { public int Id {

  • 0

I have the following types:

public abstract class Vehicle {
    public int Id { get; set; }
    public double TopSpeed { get; set; }
}

public class Car : Vehicle {
    public int Doors { get; set; }
}

public class Motorcycle : Vehicle { 
    public string Color { get; set; }
}

And I have a code-first DBContext:

public MyDbContext: DbContext { 
    public DbSet<Car> Cars { get; set; }
    public DbSet<Motorcycle> Motorcycles { get; set; }
}

This works great if I query for a car or moto directly…

var dbContext = new MyDbContext();
var cars = dbContext.Set<Car>().Where(x=> x.TopSpeed>10); // <-- THIS WORKS

But if I want a list of all vehicles, whether car or moto, I would like to do this:

var dbContext = new MyDbContext();
var vehicles = dbContext.Set<Vehicle>().Where(x=> x.TopSpeed>10); // <-- THIS DOES NOT WORK

When I try the above code, I get an exception:

System.InvalidOperationException : The entity type Vehicle is not part
of the model for the current context.

That makes perfect sense… I didn’t add Vehicle to the context. I added car and moto. At this point, I’m not sure what to do. I tried adding Vehicle to my context, but that combined the tables for car and moto into one Vehicle table. I definitely want a separate table for cars and motos (and possibly a table for the vehicle base properties as well). What’s the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:02:46+00:00Added an answer on June 3, 2026 at 5:02 am

    Have a Vehicles Properties of type DBSet of Vehicle in your MyDbContext class.

    public MyDbContext: DbContext { 
        public DbSet<Car> Cars { get; set; }
        public DbSet<Motorcycle> Motorcycles { get; set; }
        public DbSet<Vehicle> Vehicles { set; get; }
    }
    

    Now you can access all vehicles with the criteria like this

    var vehicles = dbContext.Set<Vehicle>().Where(x=> x.TopSpeed>10);
    

    Keep in mind that you should have a Key Property in your entity classes ( ID or {ClassName}ID). Otherwise it is going to give you a run time error! (Yes the code will compile.)

    public abstract class Vehicle
    {
        public int ID { set; get; }
        public double TopSpeed { get; set; }
    }
    

    EDIT : As per the comment

    By Default Entity Framework will do a Table Per Hierarchy.All Data in the hierarchy is saved in a single table and it use the Discriminator column to identify which record belongs to which subtype. So As the result you will have one Vehicle table with columns same as properties of All your classes in the hierarchy with an extra column called “Discriminator“. For a Record for Car, It will have the “Car” value inside the Discriminator column.

    enter image description here

    If you want to create Single table per each type, We will go for Table Per Type. Entity Framework will create a Table for the base class and seperate table for all child classes.

    To make this happen, You can use Fluent API to override the configuration.

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Car>().ToTable("Cars");
            modelBuilder.Entity<Motorcycle>().ToTable("Motorcycles");
    
            base.OnModelCreating(modelBuilder);
        }
    

    And the output is

    enter image description here

    Now You should be able to query Your Vehicle Entries like this

     var vehicles = dbContext.Set<Vehicle>().Where(x => x.TopSpeed > 150).ToList();
    

    And the result is here

    enter image description here

    Notice that the result contains both MotorCycle type and Car type.

    Check this link to decide what Inheritance Strategy to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following business objects: public abstract class Product { public int Id
I have the following: class Item { public object ItemId {get;set;} } class StringItem
I have the following types and classes: namespace MVC.Models public class Page { public
I have the following code: Imports MySql.Data.MySqlClient Imports MySql.Data.Types Public Class FormMain Private Sub
I have the following types [Serializable, XmlType(Namespace=http://mycompany/foo] public sealed class Limit { [XmlElement(ElementName=Value1)] public
I have the following abstract class which CAN NOT be changed. public abstract class
I have following class public abstract class Rule { protected Rule() { Nodes =
i have the following (partial) hierarchy: @MappedSuperclass public abstract class PersistentEntity { @Id @GeneratedValue(generator=system-uuid)
I have following class hierarchy: public abstract class Property<T> { private long id; private
I have the following classes: public abstract class CommandBase { ... Stuff } public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.