Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple self-referencing relationships in Entity Framework

I currently have a class called EmployeeDetails which looks like below.

public class EmployeeDetails {

    public int EmployeeDetailsId { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }

    [ForeignKey("Manager")]
    public int? ManagerId { get; set; }
    public virtual EmployeeDetails Manager { get; set; }
    [ForeignKey("LineManager")]
    public int? LineManagerId { get; set; }
    public virtual EmployeeDetails LineManager { get; set; }

}

I'm trying to add Manager and LineManager properties which will reference objects of the same type. When I try and add a migration I get the following error:

Unable to determine the principal end of an association between the types EmployeeDetails and EmployeeDetails.

The Manager property worked as expected before adding the ManagerId, LineManagerId and LineManager properties.

How can I solve it?

like image 262
James Avatar asked Sep 17 '15 11:09

James


People also ask

How do you establish a self referencing many to many relationship?

A self-referencing many-to-many relationship exists when a given record in the table can be related to one or more other records within the table and one or more records can themselves be related to the given record.

How do I add references in Entity Framework 6?

Go to references --> Add Reference --> in the dialog, choose COM and press browse. Then go to your project which is using EF and go to the projects bin folder where the EF references are stored. Select the EntityFramework.

Does PostgreSQL work with Entity Framework?

Using the Entity Data ProviderdotConnect for PostgreSQL allows using it in Entity Framework models in various ways. You can use our provider with standard Visual Studio Entity Framework tools, in the same way as SqlClient.


1 Answers

You have to specify the other side of the relationship. Like this:

public class EmployeeDetails
{

    public int EmployeeDetailsId { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }

    [ForeignKey("Manager")]
    public int? ManagerId { get; set; }

    public virtual EmployeeDetails Manager { get; set; }

    [ForeignKey("LineManager")]
    public int? LineManagerId { get; set; }

    public virtual EmployeeDetails LineManager { get; set; }

    [ForeignKey("ManagerId")]
    public virtual ICollection<EmployeeDetails> ManagedEmployees { get; set; }

    [ForeignKey("LineManagerId")]
    public virtual ICollection<EmployeeDetails> LineManagedEmployees { get; set; }

}

Generated Migration

CreateTable(
    "dbo.EmployeeDetails",
    c => new
        {
            EmployeeDetailsId = c.Int(nullable: false, identity: true),
            Name = c.String(),
            Title = c.String(),
            ManagerId = c.Int(),
            LineManagerId = c.Int(),
        })
    .PrimaryKey(t => t.EmployeeDetailsId)
    .ForeignKey("dbo.EmployeeDetails", t => t.LineManagerId)
    .ForeignKey("dbo.EmployeeDetails", t => t.ManagerId)
    .Index(t => t.ManagerId)
    .Index(t => t.LineManagerId);

Does that solve your problem?

like image 73
Fabio Luz Avatar answered Sep 27 '22 18:09

Fabio Luz