I have a class with 2 circular references to its self, .NextVersion
, and .PreviousVersion
, that creates a "chain" of objects as declared below. When trying to use Add-Migration
to create the database I get the following error:
Unable to determine the principal end of an association between the types 'SqlMetaQuery.Model.ScriptVersion' and 'SqlMetaQuery.Model.ScriptVersion'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
public class ScriptVersion
{
public Guid ScriptVersionId { get; set; }
public Guid ScriptId { get; set; }
public string Body { get; set; }
public DateTime CreatedDate { get; set; }
public User CreatedBy { get; set; }
public ScriptVersion NextVersion { get; set; }
public ScriptVersion PreviousVersion { get; set; }
}
I found several answers to similar questions that say to use .HasOptional
and .WithOptionalDependant
to resolve the associations. This works for one of the navigation properties, but fails when I try to do it for the 2nd navigation property.
modelBuilder.Entity<ScriptVersion>()
.HasOptional(v => v.NextVersion)
.WithOptionalDependent(v => v.PreviousVersion);
modelBuilder.Entity<ScriptVersion>()
.HasOptional(v => v.PreviousVersion)
.WithOptionalDependent(v => v.NextVersion);
The navigation property 'PreviousVersion' declared on type 'SqlMetaQuery.Model.ScriptVersion' has been configured with conflicting foreign keys.
What am I doing wrong?