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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:52:16+00:00 2026-05-16T08:52:16+00:00

I am trying to figure out something with EF4 Code Only. If i use

  • 0

I am trying to figure out something with EF4 Code Only. If i use TPH and i wanted to change a saved Person to Instructor or vice versa, how would i accomplish this. My POCO classes:

    public class Person
{
    public int PersonId { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }
}
public class Instructor : Person
{
    public DateTime? HireDate { get; set; }
}

public class Student : Person
{
    public DateTime? EnrollmentDate { get; set; }
}

public class Admin : Person
{
    public DateTime? AdminDate { get; set; }
}

public class PersonConfiguration : EntityConfiguration<Person>
{
    public PersonConfiguration()
    {
        this.HasKey(u => u.PersonId).Property(u => u.PersonId).IsIdentity();
        MapHierarchy()
            .Case<Person>(p => new
            {
                p.PersonId,
                p.FirstName,
                p.LastName,                    
                PersonCategory = 0
            })
            .Case<Instructor>(i => new
            {

                i.HireDate,
                PersonCategory = 1
            })
            .Case<Student>(s => new
            {
                s.EnrollmentDate,
                PersonCategory = 2
            })
            .Case<Admin>(a => new
            {

                a.AdminDate,
                PersonCategory = 3
            }).ToTable("Person");

    }
}

Lets say i have a person:

var person1 = new Person { FirstName = "Bayram", LastName = "Celik" };
context.People.Add(person1);
context.SaveChanges();

Later on i want to make this person an admin. How would i accomplish this.

var person = context.People.FirstOrDefault();
Admin test = person as Admin; // wont work

following will change the HireDate column but my discriminator field PersonCategory is still 0. So it is still not an Admin type as far as EF concerns

Admin admin = new Admin();
admin.PersonId = person.PersonId;
admin.AdminDate = DateTime.Now;

context.ObjectContext.Detach(person);
context.People.Attach(admin);
var customerEntry = context.ObjectContext.ObjectStateManager.GetObjectStateEntry(admin);
customerEntry.SetModified();
customerEntry.SetModifiedProperty("AdminDate");
  • 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-16T08:52:16+00:00Added an answer on May 16, 2026 at 8:52 am

    You can’t ever change the type of an object. You can’t do it in C#, and the EF doesn’t work around this. This is a fundamental principle of OOP.

    Instead, you need to fix your model design. As I wrote a while back:

    One of the mental barriers that you have to get over when designing a good object relational mapping is the tendency to think primarily in object oriented terms, or relational terms, whichever suits your personality. A good object relational mapping, though, incorporates both a good object model and a good relational model. For example, let’s say you have a database with a table for People, and related tables for Employees and Customers. A single person might have a record in all three tables. Now, from a strictly relational point of view, you could construct a database VIEW for employees and another one for customers, both of which incorporate information from the People table. When using a one VIEW or the other, you can temporarily think of an individual person as “just” an Employee or “just” a Customer, even though you know that they are both. So someone coming from this worldview might be tempted to do an OO mapping where Employee and Customer are both (direct) subclasses of Person. But this doesn’t work with the data we have; since a single person has both employee and customer records (and since no Person instance can be of the concrete subtype Employee and Customer simultaneously), the OO relationship between Person and Employee needs to be composition rather than inheritance, and similarly for Person and Customer.

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

Sidebar

Related Questions

I am trying to figure out something - Every time I use AVAudioPlayer I
I'm trying to figure something out. This is the code I'm using to hide
I'm new to regex and trying to figure something out for use in scala.
Well, I'm trying to figure out this thing. How would you code something like
First time working with JSON in Rails and trying to figure out something which
Trying to figure out the best way to do this: Should I do something
I'm trying to figure out how to show something like Showing 1-10 of 52
I'm trying to figure out how to do something similar to the twitter silverlight
I'm working on a little something and I am trying to figure out whether
I'm trying to figure out if something like this would be possible. We are

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.