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 7905073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:19:38+00:00 2026-06-03T10:19:38+00:00

This question is about Controllers, the code that is generated with Model association and

  • 0

This question is about Controllers, the code that is generated with Model association and inheritance using the Code First Entity Framework Approach.

I have the following Models setup with associations and inheritance.

[Table("User")]
public class User {
  [Key]
  public int UserId { get; set; }
  public string Username { get; set; }
}

[Table("UserProfile")]
public class UserProfile {
  [DatabaseGenerated(DatabaseGeneratedOption.None)]
  [Key, ForeignKey("User")]
  public int UserId { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public virtual User User { get; set; }
}

[Table("Client")]
public class Client : UserProfile {
  public bool Approved { get; set; }
}

I then generate a Controller in VS2010, setting the Template to “Controller with read/write actions and view, using Entity Framework”, Model class to “Client” and Data context class to my context class.

This generates the following Index method.

public class ClientController : Controller {
  private DataContext db = new DataContext();
  public ViewResult Index() {
    var users = db.UserProfiles.Include(u => u.User);
    return View(users.ToList());
  }
  ...
}

How come it is not like the following?

public class ClientController : Controller {
  private DataContext db = new DataContext();
  public ViewResult Index() {
    var users = db.Clients.Include(u => u.User);
    return View(users.ToList());
  }
  ...
}

Or maybe this?

public class ClientController : Controller {
  private DataContext db = new DataContext();
  public ViewResult Index() {
    var users = db.UserProfiles.OfType<Client>().Include(u => u.User);
    return View(users.ToList());
  }
  ...
}

Also, the Create method uses

db.UserProfiles.Add(client);

I was wondering why the following is not used?

db.Clients.Add(client);

Any recommendations on which approach I should be using?

  • 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-03T10:19:42+00:00Added an answer on June 3, 2026 at 10:19 am

    The answer lies in how entity framework handles inheritance in models. This scenario is called as “Table per hierarchy” inheritance. When you run the code and have a look at the tables generated, you will see that UserProfile and client data both are getting stored in one table only. As in this model a differentiator column will be used to understand what is the type object , but both the data will be stored in one table only. Hence for table per hierarchy scenario the controller stub is generated with base class model only.

    This can give you better insight :

    http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx

    Not sure why OfType() is not used, but it seems that VS only generates generalized code.

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

Sidebar

Related Questions

I have seen this question about deploying to WebSphere using the WAS ant tasks.
I've just been reading this question which is about giving an ActiveRecord model's date
I’ve got a question about the default account model, view and controller generated when
a quick question to you about the auto-generated Yii code: in the controller, the
Let me first say that I realize this question is vague and does not
This question about Timers for windows services got me thinking: Say I have (and
Followed this question about delayed_job and monit Its working on my development machine. But
This question is a follow up to my previous question about getting the HTML
I was reading this question about how to parse URLs out of web pages
I just came across this question about initializing local variables. Many of the answers

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.