Refracted Paladin
Refracted Paladin

Reputation: 12216

LightSwitch Entity: Updating the Value of Foreign Key Property

I am struggling with how I do the following in LightSwitch. I am 'intercepting' the entity during the partial void tblStaffExtendeds_Updating(tblStaffExtended entity) and then pulling some data from our local ldap and trying to set one of the properties of the entity to a specific value, like so:

partial void tblStaffExtendeds_Updating(tblStaffExtended entity)
    {

        string ldapPath = @"LDAP://DC=myLDAP,DC=myLDAP";            
        string user = entity.GPEmployeeID.ToString();
        string[] props = { 
                            ActiveDirectoryInfo.strings.DISPLAYNAME, ActiveDirectoryInfo.strings.EMAIL, 
                            ActiveDirectoryInfo.strings.LOGONALIAS, ActiveDirectoryInfo.strings.PHONE, 
                            ActiveDirectoryInfo.strings.OFFICE, ActiveDirectoryInfo.strings.TITLE, 
                            ActiveDirectoryInfo.strings.GPEMPLOYEEID
                          };

        var propResults = ActiveDirectoryInfo.UserPropertySearchByGPEmpID(user, ldapPath, props);

        entity.tblAdminStaffType.StaffType = propResults[ActiveDirectoryInfo.strings.TITLE];
        entity.WorkEmail = propResults[ActiveDirectoryInfo.strings.EMAIL];
        entity.UserID = propResults[ActiveDirectoryInfo.strings.LOGONALIAS];
        entity.WorkPhone = propResults[ActiveDirectoryInfo.strings.PHONE];
   }

Now for fields like WorkEmail and WorkPhone this works fine as those properties are just strings and that is what I am getting from LDAP.
However I do I go about setting the StaffType which is a reference to an Admin Table entry? LDAP returns a string which matches the description on the Admin Table but on the Entity I would need to set it to the correct ID, I am assuming.

Is there someway to do this, short of creating "Look Up" methods to find the ID from the Admin Table by matching the Description to my String from LDAP?

Upvotes: 1

Views: 1277

Answers (1)

Paul Keister
Paul Keister

Reputation: 13077

The solution should look something like this:

string title = propResults[ActiveDirectoryInfo.strings.TITLE];

var qryAdminStaffType = from st in DataWorkspace.ApplicationData.StaffTypes
                        where st.Title == title
                        select st;

entity.tblAdminStaffType.StaffType = qryAdminStaffType.FirstOrDefault();

In this example, I'm assuming that your data source is called ApplicationData (the LS default name), that the StaffTypes table holds your staff types, and that you are matching the Title attribute. Note that if there is no match to the title, FirstOrDefalt() will return null.

Upvotes: 1

Related Questions