Search code examples
.netlinqormbltoolkit

Mapping a code table in BLToolkit


In my database, there is one large "code" table with system code look-ups for values used all over the system. Like so:

[TableName("code_entries")]                         public class Code {
    [MapField("code_nbr")][PrimaryKey, Identity]    public int Id;
    [MapField("code")]                              public string Value;
}

I am new to BLToolkit, and am hoping that there is a concept similar to the static Mappings I have seen, but that will allow me to easily map occurrences of these codes in other tables to their respective values. For instance:

    [TableName("person")]                               public class Person {
        [MapField("person_nbr")][PrimaryKey, Identity]  public int Id;
        [MapField("dob")][Nullable]                     public int BirthDate;
        [MapField("eye_color")][Nullable]               public int EyeColorCode;
        [MapField("hair_color")][Nullable]              public int HairColorCode;    
}

If EyeColorCode and HairColorCode above map to values in the Codes table, can I create an easy way to map that data within the OR classes and obtain the whole object in a single query?

I'd like to end up with something like:

// person.Id = 1
// person.DOB = some date
// person.EyeColor = "Blue"
// person.HairColor = "Brown"

Solution

  • It's not really what you wanted but you could use Associations

    so you could add this to your Person class

    [Association(ThisKey="eye_color", OtherKey="code_nbr", CanBeNull=true)]
    public Code EyeColor;
    
    [Association(ThisKey="hair_color", OtherKey="code_nbr", CanBeNull=true)]
    public Code HairColor;
    

    And then do something like

    from p in db.Person
    select new
    {
        Id        = p.Id,
        DOB       = p.BirthDate,
        EyeColor  = p.EyeColor.Value,
        HairColor = p.HairColor.Value
    };
    

    Anyway these seem like the type of codes that almost never change I usually put these on the client at startup and then fill in the description when I display the data, makes everything a lot easier, and if I can't find an Id then I just refresh the collection