Click here to Skip to main content
15,878,814 members
Articles / Programming Languages / C#

How to Configure a Self Referencing Entity in Code First

Rate me:
Please Sign up or sign in to vote.
4.95/5 (12 votes)
4 Jun 2011CPOL2 min read 82.3K   4   9
A running example of how to configure a self reference entity with Code First

Introduction

A few days ago, a worker in Sela asked me how to configure a self referencing entity with EF Code First. In this post, I’ll show you how to implement that configuration.

How to Configure a Self Reference in Code First

Self Reference Scenarios

There are a lot of scenarios where we will want to implement a self reference between an entity to itself. For example, we do that when we want to create hierarchies in our application - an employee entity that has a self reference to his/her manager is a very common scenario for that. When we want to create that behavior in a database, all we have to do is add a foreign key in the table that points to the table primary key and we are done. But how can we do that in EF Code First?

Configure a Self Reference in Code First

In EF Code First, we can use the Fluent API in order to configure a self reference. Let's jump into an example that will direct you how to make that configuration. The first thing is the entity:

C#
public class Employee
{
  #region Properties

  public int EmployeeID { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public int? ManagerID { get; set; }
  public Employee Manager { get; set; }

  #endregion
}

As you might notice, there is nothing interesting in the entity itself – just a bunch of properties and the reference to the manager which is another employee. After we have the entity, let's create the context:

C#
public class CompanyContext : DbContext
{
  #region Properties

  public DbSet<Employee> Employees { get; set; }

  #endregion

  #region Ctor

  public CompanyContext()
    : base("Company")
  {
  }

  #endregion

  #region Methods

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Employee>().
      HasOptional(e => e.Manager).
      WithMany().
      HasForeignKey(m => m.ManagerID);
  }

  #endregion
}

In the context, you need to implement the OnModelCreating method in order to configure the self reference. We start the configuration with the HasOptional method that indicates that the employee might have a manager (or else he/she is the CEO). Then, for the manager, we configure a many side with the WithMany method. In the end, we use the HasForeignKey method to indicate the foreign key (which is the ManagerID in the example). That is it. In order to finish the example, I’ve created a database initializer that inserts some data:

C#
public class ExampleInitializer : DropCreateDatabaseIfModelChanges<CompanyContext>
{
  #region Methods

  protected override void Seed(CompanyContext context)
  {
    var manager = new Employee
    {
      FirstName = "emp1",
      LastName = "emp1",
    };
    var emp1 = new Employee
    {
      FirstName = "emp1",
      LastName = "emp1",
      Manager = manager
    };
    var emp2 = new Employee
    {
      FirstName = "emp2",
      LastName = "emp2",
      Manager = manager
    };

    context.Employees.Add(emp1);
    context.Employees.Add(emp2);
    context.SaveChanges();
  }

  #endregion
}

and a console application that runs the example:

C#
class Program
{
  static void Main(string[] args)
  {
    Database.SetInitializer(new ExampleInitializer());

    using (var context = new CompanyContext())
    {
      var query = context.Employees.Include(e => e.Manager).ToList();
      foreach (var employee in query)
      {
        Console.WriteLine("{0} {1}", employee.EmployeeID, employee.ManagerID);
      }
    }
  }
}

Here is a figure of the output:

Running Output

Summary

Let's sum up. The post includes a running example of how to configure a self reference entity with Code First. I hope it will help you if you get stuck with such a configuration.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

Comments and Discussions

 
QuestionNot working with entity framework core Pin
Kamran Shahid12-Jan-21 1:36
Kamran Shahid12-Jan-21 1:36 
QuestionTried something bellow. Do I need to do some configuration in OnModelCreating? Pin
Pubudu Karunarathne3-Feb-20 4:29
Pubudu Karunarathne3-Feb-20 4:29 
QuestionEF Update Issue? Pin
ArmyBlond1-Jun-17 4:36
ArmyBlond1-Jun-17 4:36 
PraiseJust what I was looking for Pin
TonyJPerez23-Dec-16 8:20
TonyJPerez23-Dec-16 8:20 
QuestionHow do I retrieve an entire hierarchy? Pin
Shimmy Weitzhandler26-Sep-15 9:53
Shimmy Weitzhandler26-Sep-15 9:53 
Suggestionnot working in EF6 with configurations Pin
User 481871027-May-15 8:16
User 481871027-May-15 8:16 
Suggestionnot working in EF6 with configurations Pin
User 481871027-May-15 8:16
User 481871027-May-15 8:16 
GeneralMy vote of 5 Pin
Sampath Lokuge23-Mar-14 8:51
Sampath Lokuge23-Mar-14 8:51 
GeneralRe: My vote of 5 Pin
Gil Fink12-Apr-14 0:30
Gil Fink12-Apr-14 0:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.