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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:12:27+00:00 2026-06-13T14:12:27+00:00

I am using the Entity Framework Code First in C# and I have many

  • 0

I am using the Entity Framework Code First in C# and I have many entities that have the same columns used for tracking. The columns I am using are Active, IsDeleted, CreatedBy, ModifiedBy, DateCreated, and DateUpdated. It seems tedious to have to add these properties to every entity that I want to track. I would like to have a base class that my entities can inherit from such as the one below.

public abstract class TrackableEntity
{
    public bool Active { get; set; }
    public bool IsDeleted { get; set; }
    public virtual User CreatedBy { get; set; }
    public virtual User ModifiedBy { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }
}

Then I could inherit from this class in my entities to have these properties and when the database gets generated it would have these columns for each entity.

public class UserProfile : TrackableEntity, IValidatableObject
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }

    public bool IsValid { get { return this.Validate(null).Count() == 0; } }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (String.IsNullOrEmpty(FirstName))
            yield return new ValidationResult("First name cannot be blank", new[] { "Username" });

        if (String.IsNullOrEmpty(LastName))
            yield return new ValidationResult("Last cannot be blank", new[] { "Password" });

        //Finish Validation Rules
    }
}

I really want to cut back on code duplication and use some type of method like this but I can’t get this to work. I keep receiving the error below:

Unable to determine the principal end of an association between the types ‘Namespace.Models.User’ and ‘Namespace.Models.User’. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

I have been searching around for a few days now and can’t find an answer for what I am wanting to do. I really don’t want to create a separate table that everything links to. I have read about TPT, TPH, and TPC. TPC seemed pretty close to what I want but sharing PKs across pretty much all my tables is some I definitely don’t want to do.

Here is an example of what I would like my tables to look like. These tables would be created by entities that inherit from the TrackableEntity.

[UserProfile]
    [Id] [int] IDENTITY(1,1) NOT NULL
    [FirstName] [varchar](50) NOT NULL
    [LastName] [varchar](50) NOT NULL
    [Email] [varchar](255) NOT NULL
    [Phone] [varchar](20) NOT NULL
    [CreatedBy] [int] NOT NULL
    [ModifiedBy] [int] NOT NULL
    [DateCreated] [datetime] NOT NULL
    [DateModified] [datetime] NOT NULL
    [Active] [bit] NOT NULL
    [IsDeleted] [bit] NOT NULL

[CaseType]
    [Id] [int] IDENTITY(1,1) NOT NULL
    [Name] [varchar](50) NOT NULL
    [CreatedBy] [int] NOT NULL
    [ModifiedBy] [int] NOT NULL
    [DateCreated] [datetime] NOT NULL
    [DateModified] [datetime] NOT NULL
    [Active] [bit] NOT NULL
    [IsDeleted] [bit] NOT NULL
  • 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-13T14:12:27+00:00Added an answer on June 13, 2026 at 2:12 pm

    Most likely you are having this exception because you have your User class derived from TrackableEntity as well:

    public class User : TrackableEntity
    

    The consequence is that the User entity now contains the two inherited properties

    public virtual User CreatedBy { get; set; }
    public virtual User ModifiedBy { get; set; }
    

    and Entity Framework by convention will assume a relationship between the two properties, i.e. one relationship that has those two navigation properties as its two ends. Because the navigation properties are references and not collections EF infers a one-to-one relationship. Because in the model isn’t specified which of the two navigation properties has the related foreign key for the relationship EF cannot determine what’s the principal and what’s the dependent of the relationship – which causes the exception.

    Now, this problem could be solved – as the exception tells – by defining principal and dependent explicitly. But in your model the convention – namely to assume a one-to-one relationship – is incorrect. You actually need two relationships, one from CreatedBy (with its own foreign key) and one from ModifiedBy (with another foreign key). Both relationships are one-to-many (because a User can be the creator or modifier of many other users) and don’t have a navigation collection at the other end of the relationship. You must supply a mapping with Fluent API to override the convention and define those two one-to-many relationships:

    public class UnicornsContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<CaseType> CaseTypes { get; set; }
        // ... etc.
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .HasOptional(u => u.CreatedBy)
                .WithMany()
                .Map(m => m.MapKey("CreatedBy")); // FK column name in DB table
    
            modelBuilder.Entity<User>()
                .HasOptional(u => u.ModifiedBy)
                .WithMany()
                .Map(m => m.MapKey("ModifiedBy")); // FK column name in DB table
        }
    }
    

    Note, that I have used HasOptional instead of HasRequired (which means that the FK in the database will be nullable), because – at least with autoincremented Ids – you can’t create the very first user with a CreatedBy or ModifiedBy being set – it could only refer to itself but there isn’t any valid FK value to be used because the very first PK hasn’t been created yet.

    (You could also consider to avoid the relationship and referential constraint of CreatedBy or ModifiedBy altogether and store only the user’s name as creator and modifier instead, since for auditing and tracking purposes it might be enough to save a unique person’s identity. Do you really need to navigate from every entity to its creator and modifier? Even if you need it in exceptional cases, you could still manually join to the Users table. And would it be a problem if the user gets deleted from the Users table as long as the name is still stored in CreatedBy or ModifiedBy?)

    You don’t have to deal with any inheritance mapping as long as you don’t introduce a DbSet for the abstract base class…

    public DbSet<TrackableEntity> TrackableEntities { get; set; } // NO!
    

    …or a mapping in Fluent API for that class…

    modelBuilder.Entity<TrackableEntity>()... // NO!
    

    …or use the class as a navigation property in any other class:

    public class SomeEntity
    {
        //...
        public virtual TrackableEntity Something { get; set; } // NO!
        public virtual ICollection<TrackableEntity> Somethings { get; set; } // NO!
    }
    

    With any of these EF will infer TrackableEntity as an entity and introduce an inheritance mapping between model and database tables (TPH by default). Otherwise TrackableEntity is just a base class and every property within the base class will be considered as if it were a property in the derived entity and mapped as such to a database table.

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

Sidebar

Related Questions

I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I am Using C# 2010 Entity framework code first If I have A class
I need to map a many-to-many relationship using Entity Framework Code First. Its a
I have been using Entity Framework CTP with Code-First as in this tutorial by
I have a strange problem when using Entity Framework code first. When I return
I have a small MVC 3 app using Entity Framework Code First and use
Using the Entity Framework Code First paradigm I have defined the following objects and
I'm having trouble mapping two entities together with Entity Framework CTP5 using Code First
I am using MVC3, Entity Framework v4.3 Code First, and SimpleInjector. I have several
I am using Entity Framework code-first. I have a table the will not exceed

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.