How to create Lookup table and define relationships

后端 未结 1 378
遥遥无期
遥遥无期 2020-12-06 14:24

As you can see at below, there are a Lookup table for the enum values and I want to create a relationship between a table\'s enum values and LookupKey colum

相关标签:
1条回答
  • We have a common lookup table here. It looks simlar to yours. LookupData has a primary key and a foreign key to LookupTypes which is equivalent to your enum and the value. We might also have some other simple fields like a flag or code which are identified in the LookupType metadata table. Then in out main table we might have "GenderLookupId" which points to the LookupData.Id field. The IDs themselves have no meaning and can be entered in any order. If you want gender 1 and 2 to have meaning, you should probably add another attribute for that (see surrogate keys).

    Example with data:

    LookupType

    ID    Description    CodeDesc        BooleanDesc  
    1     Genders        Gender Code     NULL
    2     Races          Race Code       Is Active
    

    LookupData

    ID    LookupTypeId    Description    Code    Boolean
    789   1               Male           M       NULL
    790   2               White          W       True
    791   1               Female         F       NULL
    792   2               Hispanic       H       False
    

    Main Name Table

    NameId   Name          GenderLookupId   RaceLookupId
    1234     Joe Smith     789              790
    1235     Mary Meyers   791              792
    

    Classes:

    public class LookupType
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public string CodeDescription { get; set; }
        public string BooleanDescription { get; set; }
    

    }

    public class LookupData
    {
        public int Id { get; set; }
        public int LookupTypeId { get; set; }
        public string Description { get; set; }
        public string Code { get; set; }
        public bool? BooleanValue { get; set; }
        public LookupType LookupType { get; set; } 
    

    }

    public class Name
    {
        public int Id { get; set; }
        public string FullName { get; set; }
        public int? GenderLookupId { get; set; }
        public LookupData Gender { get; set; } 
    }
    

    LookupData Config:

    HasRequired(p => p.LookupType).WithMany(p=>p.LookupData).HasForeignKey(p=>p.LookupTypeId).WillCascadeOnDelete(false);
    

    Name Config:

    HasOptional(p => p.Gender).WithMany(p=>p.Name).HasForeignKey(p=>p.GenderLookupId).WillCascadeOnDelete(false);
    
    0 讨论(0)
提交回复
热议问题