I am new to the entity framework, and C# but not SQL. I have table [Contact] with multiple fields referencing a lookup table called [XRef_Codes]. First, I want to bind all dropdownlists on an asp.net webform to the filtered lookup table on page load. Second, I want to be able to expose the lookup values (integers and text value) in the contact entity model (see below)
Here is a sample of fields in the
[Contact] table:
FirstName (vchar(50)
LastName (vchar(50)
xPrefix (int)
xSuffix (int)
xReferral (int)
xStatus (int)
xGender (int)
xMaritalStatus (int)
xAgeGroup (int)
I have a self-referencing lookup table called [xRef_Codes] that has the pick values for all the fields prefixed “x” in the contact table (i.e. xPrefix, xSuffix):
[xRef_Codes]
CodeID (int)
CodeName (vchar50)
OfCodeID (int)
SortOrder (int)
SetAsDefault (bit)
[1] In my asp.net form, I want to populate a sorted/filtered dropdownlists FROM xRef_Codes table WHERE OfCodeID=## ORDER BY SetAsDefault, SortOrder. In the page load, I would bind all of the dropdownlists at one time.
[2] I want to associate the related XRef_Codes “CodeName” value as part of the contact object/entity. So I could see that contact.prefix.CodeName =”Mr.” and contact.suffix.CodeName = “Sr.” and contact.gender.CodeName =”Male”, etc.
What is the best approach to this? Can some help me understand if I need to correlate all these fields in the entity framework designer?
I am new to things, so I thought I would share the fruits of my journey. My solution was to utilize the global.asax file, and the Session_Start() object.
First, I created a class for my dropdown list codes called xRefCodesRepository. In my first routine I load the values into a session variable like this:
Second, I add a class routine to fetch my codes by a parentID value. In my case, I have a self-referencing table with fields like CodeID, CodeName, OfCodeID, etc. I fetch my codes like this:
Next, I edit my global.asax file. I reference my class folder i.e. using MyDatabase.CLASSNAME;
Then I add a routine in my global.asax that calls the class routine that loads the codes into a session variable:
Finally, in my code I reference the session cache to populate various objects.