An alternative lookup table approach needed to make C# models more generic
I currently have the following Models in my EF Code First MVC project
(edited for brevity):
public class Car
{
public int Id { get; set; }
public string Descrip { get; set; }
// Navigation Property.
public virtual CarColour CarColour { get; set; }
... + numerous other navigation properties.
}
public class CarColour
{
public int Id { get; set; }
public string ColourName { get; set; }
}
The CarColour table in the DB contains many rows.
In my project, I have about 10 of these sorts of tables, which are
essentially lookup tables.
Rather than have 10 lookup tables (and 10 corresponding 'hard' types in
code), I was tasked with implementing a more re-usable approach, instead
of having loads of lookup tables, specific to Car (in this example), along
the lines of having a couple of tables, one of which may hold the item
types (colour, fuel-type etc.) and one which contains the various values
for each of the types. The idea being that our model will be able to be
re-used by many other projects - some of which will have potentially
hundreds of different attributes, and as such, we won't want to create a
new Class/Type in code and generate a new lookup table for each.
I am having difficulty in understanding the c# implementation of this sort
of approach and hope someone may be able to give me an example of how this
can be achieved in code, more specifically, how the above models would
need to change, and what additional classes would be required to
accomplish this?
No comments:
Post a Comment