Background
Microsoft’s LightSwitch is marketed as this easy to use tool, but I am finding it as a great easy to confuse tool.
Problem Identification
GUI
I have a simple Data Entry GUI that looks like this:
I am being asked to enter the “added By” name.
Usually I will check on the database’s stored procedure and set it to SYSTEM_USER when empty.
Data Model
BTW, here is the entity..
We can see that “addedBy” is a String and it is Required.
Unfortunately, I am not able to toggle the required flag as it is greyed out.
Remediation
Entity – properties – Allow Empty String
Property Changes
Here is our first remediation try …
The “Is Required” flag is disabled. And, so took the easy route of checking the “Allow Empty String” flag.
And, so now that we are “Allowing Empty String”, I feel good.
Trial
Tried it out and here is where that got me…
GUI
Textual
Failed to load model: Model Error: Could not resolve id ‘:@AllowEmptyString’ to a model item.
Solve the error message
Guide
The rest of the afternoon was spent googling for this incredibly useful error message
Failed to load model: Model Error: Could not resolve is ‘:@AllowEmptyString’ to a model item.
Best idea comes through Dave Baker and it is pasted here:
Here are the steps:
- Launch your favorite editor
- Identify the lsml file
- Syntax
- [Project]\[Project].HTMLClient\Properties\<datasource>\[entity].lsml
- Sample
- EDiag\EDiag.HTMLClient\Properties\DBDiagData\listofContacts.lsml
- Syntax
- Comment out the DiscardFromCollection element
Guide
Faulty
Revised
Entity – properties – Default to single space
Guide
The alternate route is to revisit our entity and take the following steps:
- Supporting Work
- Unfortunately, Javascript does not provide a user’s name
- We have to get on the Server Side
- To get it on the Server side we have to tighten things up and require Windows or Forms Authentication
- Entity
- For the Entity provide code for setting the front-end default value
- And, if need be the actual backend value
Project Properties
From the Solution Explorer, choose the Project, and right click on it
Default Screen
Here is the default setting
Explanation
- The default choice is not to enable authentication
Revised Screen
Explanation
- The Authentication Type is
- Use Windows authentication
- We have two choices
- Allow only users and Active Directory security groups specified in the Users screen of the application
- Allow any authenticated Windows user
- We chose to “Allow any authenticated Windows user” as this is just a mock up
Entity
- Access the HTMLClient Tab
- Choose “Write Code“
- For the Created Event
- And, enter a code that supplies a place holder (space) for our property
- Access the Server Tab
- Choose “Write Code“
- In the Inserting Event
- Check if field is still at default, if so calculate value and overwrite default
HTMLClient
Code
/// <reference path="~/GeneratedArtifacts/viewModel.js" /> myapp.listofContact.created = function (entity) { // Write code here. entity.active = true; entity.addedOn = new Date(); //Add a single space var STRING_SPACE = " "; entity.addedBy = STRING_SPACE; };
Server
Entity – ListofContact
Code
partial void listofContacts_Inserting(listofContact entity) { //when inserting, set added By to current windows user //If added By is still at default //entity.addedBy = entity.username; if (entity.addedBy == " ") { //If user is authenticated, then get username if (this.Application.User.IsAuthenticated) { entity.addedBy = this.Application.User.Name; } // if (this.Application.User.IsAuthenticated) } // if (entity.addedBy == " ") } // listofContacts_Inserting