简体   繁体   中英

How can I setup an entity with two foreign keys to itself?

I have an entity that has two foreign keys to it's own table. This is what it looks like:

public class SystemUser : BaseModel
{
    [Column("SystemUserId")]
    public override Guid ID { get; set; }

    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string MobilePhone { get; set; }
    public Guid? MobilePhoneProviderId { get; set; }
    public virtual MobilePhoneProvider MobilePhoneProvider { get; set; }
    public Guid? ManagerId { get; set; }
    public virtual SystemUser Manager { get; set; }
    public Guid? AdminAssistantId { get; set; }
    public virtual SystemUser AdminAssistant { get; set; }

    public bool IsActive { get; set; }
    public bool SendEmail { get; set; }

    public string FinaleUsername { get; set; }
    public string FinalePassword { get; set; }
    public int? FloatUserId { get; set; }

    public Guid? SystemUserTypeId { get; set; }
    public virtual SystemUserType SystemUserType { get; set; }
    public Guid? SystemUserJobFunctionId { get; set; }
    public virtual SystemUserJobFunction SystemUserJobFunction { get; set; }
    public Guid? SystemUserJobRoleId { get; set; }
    public virtual SystemUserJobRole SystemUserJobRole { get; set; }
    public Guid? SystemUserLocationId { get; set; }
    public virtual SystemUserLocation SystemUserLocation { get; set; }

    public virtual ICollection<SystemUserRole> Roles { get; set; }
}

Note the AdminAssistant and Manager properties. However it is giving me the following error:

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

If I remove one of them (either one, it doesn't matter which), it works fine but for some reason it is getting confused about having both. I tried adding the ForeignKey attributes on each as well as using the fluent API to configure it like this:

modelBuilder.Entity<SystemUser>()
            .HasOptional(x => x.Manager)
            .WithMany()
            .HasForeignKey(x => x.ManagerId);

modelBuilder.Entity<SystemUser>()
            .HasOptional(x => x.AdminAssistant)
            .WithMany()
            .HasForeignKey(x => x.AdminAssistantId);

Neither worked. Is there any way I can do this?

FYI I'm not using Code First. I'm writing the database script manually and just using EF as an ORM. Not sure if that information is relevant but I thought I'd give it anyways.

Looks like you will want to include something like

ICollection<SystemUser> ManagedUsers {get; set;}
ICollection<SystemUser> AdminAssistedUsers {get; set;}

Then you should be able to do something like this:

modelBuilder.Entity<SystemUser>()
        .HasOptional(x => x.Manager)
        .WithMany(x => x.ManagedUsers)
        .HasForeignKey(x => x.ManagerId)
        .WillCascadeOnDelete(false);

modelBuilder.Entity<SystemUser>()
        .HasOptional(x => x.AdminAssistant)
        .WithMany(x => x.AdminAssistedUsers)
        .HasForeignKey(x => x.AdminAssistantId)
        .WillCascadeOnDelete(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM