Tejana

Microsoft LightSwitch Entity Names and Properties Self Populating Tables

Posted in LightSwitch by tejana on May 15, 2011

This exercise will demonstrate a project with editable  grid screens for entity names and properties which self populate and update automatically. We set up our model by starting a new project named lsEntityPropertyNames and adding a new table named EntityName with one property “Name”,  then adding another table “PropertyName” with one property “Name” and one relationship to “EntityName”.  We then add editable grid screens for each entity, adding the EntityNames to the EditablePropertyNamesGrid as a data item.

lsPropertyNames

Open the EditableEntityNamesGrid in the screen designer and write code for the EditableEntityNamesGrid_Activated():

foreach (string s in
                this.Application.Details.GetModel().GlobalItems
                .OfType<Microsoft.LightSwitch.Model.IEntityType>()
                .Select(e => e.Name)
                .Except(EntityNames.Select(e => e.Name)))

            EntityName n = new EntityName();
            n.Name = s; 
}

Open the EditablePropertyNamesGrid and write code for the EditablePropertyNamesGrid_Activated:

//property name does not exist while entity name does
foreach (EntityName n in EntityNames)
{
     Microsoft.LightSwitch.Model.IEntityType entityType =
          this.Application.Details.GetModel()
               .GlobalItems
               .OfType<Microsoft.LightSwitch.Model.IEntityType>()
               .Single(e => e.Name.Equals(n.Name, StringComparison.Ordinal));

     foreach (string s in entityType.Properties.Select(p => p.Name)
               .Except(PropertyNames
               .Where(pn => pn.EntityName.Equals(n))
               .Select(pn => pn.Name))
     {
          PropertyName _propertyName = new PropertyName();
          _propertyName.Name = s;
          _propertyName.EntityName = n;
     }
}

//entity name does not exist
foreach (string s in
             this.Application.Details.GetModel().GlobalItems
             .OfType<Microsoft.LightSwitch.Model.IEntityType>()
             .Select(e => e.Name)
             .Except(EntityNames.Select(e => e.Name)))
{
     {
          EntityName n = new EntityName();
          n.Name = s;
          Microsoft.LightSwitch.Model.IEntityType entityType =
               this.Application.Details.GetModel()
               .GlobalItems
               .OfType<Microsoft.LightSwitch.Model.IEntityType>()
               .Single(e => e.Name.Equals(s, StringComparison.Ordinal));

          foreach (string s2 in
               entityType.Properties.Select(p => p.Name)) 
          {
               PropertyName pn = new PropertyName();
               pn.Name = s2;
               pn.EntityName = n;
          }
     }
}

 

 

Press F5,

run3

 

These tables self populate when open. If the entries are saved they will not duplicate. If the entries are deleted, they will reappear next time the screen is opened. If entities are added to the project their properties will appear the next time the screens are opened.

To exclude navigation properties insert the code in bold: 

//property name does not exist while entity name does
foreach (EntityName n in EntityNames)
{
     Microsoft.LightSwitch.Model.IEntityType entityType =
          this.Application.Details.GetModel()
               .GlobalItems
               .OfType<Microsoft.LightSwitch.Model.IEntityType>()
               .Single(e => e.Name.Equals(n.Name, StringComparison.Ordinal));

     foreach (string s in entityType.Properties.Select(p => p.Name)
               .Except(PropertyNames
               .Where(pn => pn.EntityName.Equals(n))
               .Select(pn => pn.Name)
               .Union(entityType.NavigationProperties.Select(np => np.Name))))
     {
          PropertyName _propertyName = new PropertyName();
          _propertyName.Name = s;
          _propertyName.EntityName = n;
     }
}

//entity name does not exist
foreach (string s in
             this.Application.Details.GetModel().GlobalItems
             .OfType<Microsoft.LightSwitch.Model.IEntityType>()
             .Select(e => e.Name)
             .Except(EntityNames.Select(e => e.Name)))
{
     {
          EntityName n = new EntityName();
          n.Name = s;
          Microsoft.LightSwitch.Model.IEntityType entityType =
               this.Application.Details.GetModel()
               .GlobalItems
               .OfType<Microsoft.LightSwitch.Model.IEntityType>()
               .Single(e => e.Name.Equals(s, StringComparison.Ordinal));

          foreach (string s2 in
               entityType.Properties.Select(p => p.Name)
                    .Except(entityType.NavigationProperties.Select(np => np.Name)))
          {
               PropertyName pn = new PropertyName();
               pn.Name = s2;
               pn.EntityName = n;
          }
     }
}

To complete the desired behavior remove the add and edit buttons from the screen grids and set the entity properties to read only if not null. Source.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: