I am working with a legacy database that cannot have its structure changed (at least not yet), and it has something like this:
**Profile**:
profile_id
first_name
last_name
**OneBigLookupTableToRuleThemAll**:
lookup_id (PK)
category_id (PK) (refers to a recrod in a OneBigCategoryTableToRuleThemAll)
description (basically a label)
**ProfileProperties**:
property_key (PK GUID)
profile_id
lookup_id
UNIQUE constraint on profile_id, lookup_id
As an example of two categories:
Degrees (MD, PhD, MS, MPH, etc) -- cat_id 1, for example
Job Responsibilities (Statistician, Medical Doctor, Epidemiologist, etc) -- cat_id 2
So, in the Lookup table, we end up with stuff like this:
lookup_id, cat_id, description
1 , 1 , MD
2 , 1 , PhD
3 , 1 , MS
4 , 1 , MPH
5 , 2 , Statistician
6 , 2 , Medical Doctor
7 , 2 , Epidemiologist
Thus, in the ProfileProperties table we end up with stuff like:
property_key, profile_id, lookup_id
some guid , 1 , 1 -- MD degree
some guid , 1 , 4 -- MPH degree
some guid , 1 , 6 -- Medical Doctor
I would like to have an entity like this:
public class Profile {
public int ProfileId { get; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<JobResponsibility> JobResponsibilities { get; set; }
public ICollection<Degree> Degrees { get; set; }
}
public class JobResponsibility {
public int Id { get; set; }
public string Description { get; set; }
}
public class Degree {
public int Id { get; set; }
public string Description { get; set; }
}
— Actually, in the setters of the Id, I would like to constrain the value to one of the actual values that is in the database, though I’m not sure I’d go to full blown Enum-type support (and I know code first does not support that yet)
I believe one of the EntityFramework mapping scenarios should be able to handle this, but I’m having trouble figuring out what I need to do to get this to work.
Anyone have any tips or resources I can read up on this kind of scenario? I’m thinking it’s actually going to end up very basic, but I’m new to EF.
Thank you,
Josh
I don’t think that any entity framework mapping (especially if we talk about fluent / code-first) is able to map that directly – I just tested approach with inheritance and it didn’t work. EF is heavily dependent on the way how database is designed. In your case you will most probably end with something like this:
Now if you want to have properties like Degrees you will have to add non mapped property like:
It will work only for reading but not for modifying properties. For modification you will need more complex logic to build correct structure inside real
Propertiescollection.Even support of enums planned to .NET 4.5 would not solve it. Enums will support only representing single integral column as enum but in your case you need to decompose many to many relation to many enums.