0
votes

I have 3 foreign key ids and they are coming from same table, so they are referenced with table names in the below class, now - how can I know which object refers to which ID - for example the Ids

    public partial class InspectionResult
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public InspectionResult()
        {
            this.EventLogs = new HashSet();
            this.InspectionItems = new HashSet();
            this.InspectionResultStatCounts = new HashSet();
            this.NOVs = new HashSet();
            this.UploadedDocuments = new HashSet();
            this.AspNetUsers = new HashSet();
        }

        public int InspectionResultId { get; set; }

        public string EnteredById { get; set; }

        public string CreatedByUserId { get; set; }
        public string UpdatedByUserId { get; set; }

        public virtual AspNetUser AspNetUser { get; set; }
        public virtual AspNetUser AspNetUser1 { get; set; }
        public virtual AspNetUser AspNetUser2 { get; set; }

        public virtual ICollection AspNetUsers { get; set; }
    }

EnteredById, CreatedByUserId, UpdatedByUserId are coming from same table AspNetUser, but how would I know which Id belongs to AspNetUser, AspNetUser1, ,AspNetUser2 and how to handle AspNetUsers object? I am using EF Database First Approach any help please - thank you.

1

1 Answers

2
votes

You can use the "ForeignKey" data annotation to signify which Id belongs to which navigation property. I would suggest you name your navigation properties with meaningful names (see below) instead of just 1, 2, 3.

public string EnteredById { get; set; }
public string CreatedByUserId { get; set; }
public string UpdatedByUserId { get; set; }

[ForeignKey("EnteredById")]
public virtual AspNetUser EnteredBy { get; set; }

[ForeignKey("CreatedByUserId")]
public virtual AspNetUser CreatedBy { get; set; }

[ForeignKey("UpdatedByUserId")]
public virtual AspNetUser UpdatedBy { get; set; }