Unable to add a second self-binding FK to the model, causes Unable to determine the main error of the end

Firstly, I know that there are many Unable to determine the principal end of an association between the types error Unable to determine the principal end of an association between the types , but ever the one that I see does not correspond to my problem if I missed one, sorry for that.

I built Entity, which ultimately refers to it myself twice, and when I put the code in the first link, it works fine, as soon as the ad code for the second one breaks. Performing some testing, I found that if I use them myself, then everything works fine only when I add a second self-promotion, which it breaks. The code I use for my own links:

  [ForeignKey("ManagerID")] public User Manager { get; set; } //Auditing Fields public DateTime DateCreated { get; set; } public int? UpdatedByUserID { get; set; } public DateTime? DateUpdated { get; set; } public DateTime LastAutoUpdate { get; set; } [ForeignKey("UpdatedByUserID")] public User UpdatedByUser { get; set; } 

Full code of the object code:

 public class User { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public int ADPFileNumber { get; set; } [Required] public string ADUserName { get; set; } public int AirCardCheckInLateCount { get; set; } [Required] public string Email { get; set; } public DateTime? EndDate { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } public int ManagerID { get; set; } public string MobilePhone { get; set; } [Required] public string Office { get; set; } [Required] public string Phone { get; set; } public decimal PTO { get; set; } public DateTime StartDate { get; set; } public int VehicleCheckInLateCount { get; set; } public int WexCardDriverID { get; set; } [ForeignKey("ManagerID")] public User Manager { get; set; } //Auditing Fields public DateTime DateCreated { get; set; } public int? UpdatedByUserID { get; set; } public DateTime? DateUpdated { get; set; } public DateTime LastAutoUpdate { get; set; } [ForeignKey("UpdatedByUserID")] public User UpdatedByUser { get; set; } } 

What am I missing because the second self-promotion is interrupted?

+5
source share
2 answers

You must specify the main end of both associations explicitly. You can do this with the class that you originally had, without the inverse properties of the collection:

 modelBuilder.Entity<User>() .HasOptional(u => u.Manager) .WithMany() .HasForeignKey(u => u.ManagerID); modelBuilder.Entity<User>() .HasOptional(u => u.UpdatedByUser) .WithMany() .HasForeignKey(u => u.UpdatedByUserID); 

Note that ManagerID must be int? . You cannot create any User if another user is required for this. This is a problem with chicken and egg.

+2
source

As mentioned in Multiple Relationships for Binding in the Entity Framework , you seem to be missing another part of the relationship.

i.e.

 [InverseProperty("Manager")] public virtual ICollection<User> ManagedUsers {get;set;} [InverseProperty("UpdatedByUser")] public virtual ICollection<User> UpdatedUsers {get;set;} 

EDIT: based on @Gert Arnold answer you really have to add the [InverseProperty] attribute

0
source

Source: https://habr.com/ru/post/1243954/


All Articles