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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:56:05+00:00 2026-05-22T11:56:05+00:00

I am trying to model a sort of multiple inheritence relationship with EF 4.1

  • 0

I am trying to model a sort of “multiple inheritence” relationship with EF 4.1 Code First. Here is an example what I am trying to do.

Let’s say I am attempting to model the way a user interacts with my application using a “User” object. This, being the base class, is used to describe the current user when they aren’t doing anything in particular (such as visiting the homepage). It may look like this:

public class User
{
    public Guid ID { get; set; }    // Just use the forms authentication user ID
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Now, if I want to create a representation of that same user but in a different portion of the site, say, as a shopper, it may look like this:

public class Shopper : User
{
    public virtual ICollection<Orders> Orders { get; set; }
}

And so on, and so forth. When I go to insert a Shopper that has a pre-existing User entry, it throws an exception because the PK is already taken in the User table.

Is there any way to model this (IsA) relationship with EF Code First? Or am I going to be stuck with something like this?

public class Shopper
{
    public Guid UserID { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<Order> Orders { get; set; }

    public string FirstName
    {
        get { return User.FirstName; }
        set { User.FirstName = value; }
    }

    // yada, yada, yada...
}

I would like to stick with Code First and model the relationships right in my DbContext, but I can’t figure out quite how to do something like this. Thanks!

EDIT:

So, I am trying to do something like this:

public void SomeMethod ()
{
    var UserID = Guid.NewGuid ();
    var MyUser = new User () { ID = UserID };
    SaveUserToDatabase (MyUser);

    var ShopperRepresentation = GetUserAsShopper (UserID);
    //  Do stuff.
}

Basically like using object-oriented roles, I guess. I want to use the same PK for every represenation of that user, but store all of their basic information in a base class called User. I know this is possible if I write my own SQL, of course, but I want to see if EF Code First can do it, too.

  • 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-05-22T11:56:05+00:00Added an answer on May 22, 2026 at 11:56 am

    Yes, you can do it the way you describe in your first two code examples.

    I think you just need to define a mapping, which you’ll want to do in your OnModelCreating function of your DataContext in addition to having your classes set up right. How you do it depends on what mapping scheme you’re using. I went for Table-Per-Type (TPT) in my most recent project, so I had something like this:

    modelBuilder.Entity<User>().ToTable("Users");
    modelBuilder.Entity<Shopper>().ToTable("Shoppers");
    modelBuilder.Entity<OtherUser>().ToTable("OtherUsers");
    

    Let me know if that doesn’t work for you and I’ll see what I can do.

    Edit:

    Having seen your clarification below, I can’t think of a way to do that. You’d have to keep each objects stored separately (having EF treat a Shopper as just a Shopper, not a Shopper and a User), even though they share common data. That could lead to data mismatches (if, say, Shopper got its LastName updated but User didn’t). I think you might be better off going with something like:

    public class User
    {
        public virtual Guid ID { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual ShopperInfo { get; set; }
    }
    
    public class ShopperInfo
    {
        public virtual ICollection<Order> Orders { get; set; }
    }
    

    and then when you need to treat User as a Shopper, you just access the ShopperInfo (and if its not there, you create it). EF will be able to properly set that up for you no problem.

    Though if you’re going to have a lot of types of users, that might get cumbersome. Just a suggestion though – I think its a bit cleaner.

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

Sidebar

Related Questions

I'm trying to sort a view model binding on multiple properties. The problem is
I'm trying setup a generic sort of web of related objects. Let say I
I'm trying to set up a custom sort behaviour for my model/view structure in
I am trying to model a tree relationship in a table. For instance, there
I am trying to create a 'sort by status' function that shows, for example
I am trying to implement some sort of nested collections in a Backbone.Model To
I'm trying to bake out a sort of single table inheritence a.k.a. table per
I'm trying to sort a model of mine in a view by a value
I am trying to sort my ArrayList, but when I check the first item
Trying to rephrase/clean up question here: I'm trying to do some sort of conditional

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.