如何知道哪个ID属于哪个对象?

时间:2020-03-30 22:29:29

标签: c# entity-framework

我有3个外键ID,它们来自同一张表,因此在下面的类中使用表名对其进行引用-现在,我如何知道哪个对象引用了哪个ID-例如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来自同一表AspNetUser,但是我怎么知道哪个ID属于AspNetUser,AspNetUser1,AspNetUser2以及如何处理AspNetUsers对象?我正在使用EF数据库优先方法有任何帮助-谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用“ ForeignKey”数据注释来表示哪个ID属于哪个导航属性。我建议您使用有意义的名称(请参见下文)命名导航属性,而不只是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; }