2

When I'm trying to create a migration, Entity Framework throws an error

Unable to determine the principal end of an association between the types 'WorkFlowState' and 'WorkFlowState'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Code:

public class WorkFlowState
{
    public Guid Id { get; set; }

    public virtual WorkFlowState NextState { get; set; }
    public virtual WorkFlowState PrevState { get; set; }
}

What should I do?

Update 1: People are telling that the question is kind of duplicated question, but if you look at the accepted answer ( the third option which octavioccl provided ) you will see how it is different.

Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53
  • 1
    There are a lot of similar questions on SO, [here's one of them](http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr), [another one](http://stackoverflow.com/questions/23505201/unable-to-determine-the-principal-end-of-an-association-entity-framework-model). – Michael Aug 11 '15 at 10:07

1 Answers1

3

The problem is EF is trying to configure by convention an one-to-one relationship. If you check the link that was shared by @Michael in his comment, you will notice that you need to specify who is the principal end and who is the dependent end. When you are going to create a new instance of WorkflowState you must set always the principal end. Now, if you need to configure an one to one relationship, you will notice by that link you have two options:

Option 1: Specifying the FK of your relationship

public class WorkFlowState
{
     public Guid Id { get; set; }

     [Key,ForeignKey("PrevState")]
     public Guid PrevStateId { get; set; }
     public virtual WorkFlowState NextState { get; set; }
     public virtual WorkFlowState PrevState { get; set; }
}

Option 2: Using the Required data annotation:

public class WorkFlowState
{
     public Guid Id { get; set; }

     public virtual WorkFlowState NextState { get; set; }
     [Required]
     public virtual WorkFlowState PrevState { get; set; }
}

But there is a third option in case you need both references as optional:

public class WorkFlowState
{
     public Guid Id { get; set; }

     [ForeignKey("PrevState")]
     public Guid? PrevStateId { get; set; }

     [ForeignKey("NextState")]
     public Guid? NextStateId { get; set; }

     public virtual WorkFlowState NextState { get; set; }
     public virtual WorkFlowState PrevState { get; set; }
}

In this case you are going to create two unidirectional relationships. For help you understand better what happens in this last case, the Fluent Api configurations of these relationships would be this way:

modelBuilder.Entity<WorkFlowState>().HasOptional(t => t.NextState).WithMany().HasForeignKey(t => t.NextStateId);
modelBuilder.Entity<WorkFlowState>().HasOptional(t => t.PrevState).WithMany().HasForeignKey(t => t.PrevStateId);
Community
  • 1
  • 1
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • 1
    Wow, I used the third option and it works well! :) There are a couple of awesome people in the world, you are one of them! I learned much from you ... Thanks. – Ali Bahrami Aug 11 '15 at 14:18