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

  • Home
  • SEARCH
  • 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 7056743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:54:12+00:00 2026-05-28T03:54:12+00:00

I have a problem With Entity Framework and TPC Inheritance With Self-Referencing Child Class.

  • 0

I have a problem With Entity Framework and TPC Inheritance With Self-Referencing Child Class.

My Class definitions:

Public Class BaseObject
    <Key()>
    <DatabaseGenerated(DatabaseGeneratedOption.None)>
    Public Overridable Property iId As Integer

    <Required()>
    Public Property iModUserId As Integer

    <ForeignKey("iModUserId")>
    Public Property ModUser As User

    <Required()>
    Public Property dtModDate As DateTime
End Class

Public Class User
    Inherits BaseObject

    <Required()>
    Public Property sFirstName As String

    <Required()>
    Public Property sLastName As String
End Class

This is my Context

Public Class DBTestContext
    Inherits DbContext

    Public Property BaseObjects As DbSet(Of BaseObject)

    Protected Overrides Sub OnModelCreating(modelBuilder As System.Data.Entity.DbModelBuilder)
        modelBuilder.Entity(Of User)().Map(Sub(m)
                                               m.MapInheritedProperties()
                                               m.ToTable("tUsers")
                                           End Sub)
    End Sub

End Class

The problem is that Entity Framework sets the Foreign Key (FK) relationship to the table that is created for the “BaseClass”, and NOT to the table created for the “User” class.

This is a major problem since nothing is supposed to be inserted to the “BaseClass” table, and therefore the foreign key constraint is violated when I insert a new user:

Dim context As DBTestContext = New DBTestContext()

Dim user1 As User = New User()

user1.iId = 1
user1.iModUserId = 1
user1.dtModDate = DateTime.Now

context.Users.Add(user1)
context.SaveChanges() 'Error occurs here.

The code works perfectly if I remove the inheritance. The code also work if I remove the DBSer for “BaseObject” and then no table is created for BaseObject, wich is good, but then I cannot easily do a search for all “BaseObjects”, wich is bad (but not essential).

The problem with this “solution” is that when I add the code below for more class definitions than the a table for the “BaseObject” class reappears (do not know why, can somebody explain?) and the foreign key relationship is once again set to the “BaseObject” table.

Public Class BaseObject
    <Key()>
    <DatabaseGenerated(DatabaseGeneratedOption.None)>
    Public Overridable Property iId As Integer

    <Required()>
    Public Property iModUserId As Integer

    <ForeignKey("iModUserId")>
    Public Property ModUser As User

    <Required()>
    Public Property dtModDate As DateTime

    <InverseProperty("BaseObjects")>
    Public Overridable Property Group As ICollection(Of Group)
End Class

Public Class User
    Inherits BaseObject

    <Required()>
    Public Property sFirstName As String

    <Required()>
    Public Property sLastName As String
End Class

<Table("tGroups")>
Public Class Group
    Inherits BaseObject

    <InverseProperty("Groups")>
    Public Overridable Property BaseObjects As ICollection(Of BaseObject)
End Class

Is there any way to fix this so that the foreign key relationship is set to the “User” table (Class), and not to the “BaseObject” table?

I have tried with the below Fluent API, but the result is the same:

modelBuilder.Entity(Of User)().HasRequired(Function(u) u.ModUser).WithMany().HasForeignKey(Function(u) u.iModUserId)
  • 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-28T03:54:12+00:00Added an answer on May 28, 2026 at 3:54 am

    I don’t think you can use relation to base class of TPC inheritance. It doesn’t make sense and IMHO it cannot work. The relationship must be expressed in database as any other valid relationship = referential constraint and foreign key. How should database manage relation with non existing table? TPC means that there should not be any table for parent entity because whole content of the parent entity is mapped to the table for every child entity so self referencing relation to parent entity cannot be modeled. It would lead to special set of FKs from derived entity to all other derived entities. Because EF doesn’t know how to handle it, it fails.

    I expect you must either change your design or use TPT inheritance.

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

Sidebar

Related Questions

I have a problem updating a foreign key in an Entity Framework entity. I
I have a following problem. I have a class in the entity framework model
I have a problem with the following Linq query using Entity Framework: from o
I'm using Entity Framework in my project, and I have the problem that, once
I have a problem with a query using hibernate. The entity class EntityClass has
I have problem with entity framework and mysql. I have solved entity framework function
I have problem adding entity framework model to my project. Here is what I
I'm using entity framework and i have a problem that this dropdown ignores my
I got a problem with Entity Framework 4.0 I have a hierarchical table Category:
i have a problem with this code: public partial class KnihovnyForm : Form {

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.