Up Create, modify and delete code samples
DevForce 2010 Resource Center » Code samples » Create, modify and delete code samples » Code sample: Adding and deleting (Silverlight)

Code sample: Adding and deleting (Silverlight)

Last modified on August 15, 2012 17:21

This code samples presents a small Silverlight application with simple data editing capabilities to add, edit and delete Customer entities.


Problem

Once you've queried entities into the EntityManager cache, you'll want to work with those entities.  How to display the entities in a grid, and provide editing and deletion capabilities?

Solution

This solution builds upon a simple query explorer type of interface to provide simple editing capabilites for Customer entities.

Create

In this solution, a Create() factory method is implemented in the Customer class.  The Create method constructs an instance of Customer, sets its EntityKey and a few other properties, and then adds the entity to the EntityManager.  For other approaches to entity creation, see here.  

Since the Customer type in this model (NorthwindIB) uses a GUID id, in this Create method we set the key value directly. However, that is one of three different approaches that can be used to assign an EntityKey value.

 Approach to Setting Key ValueWhen Appropriate
Set it directly Key is a GUID
Database-assigned The primary key of the entity’s backing table is an auto-generated identity (SQL Server), sequence (Oracle) or some other store-generated value.
3Invoke a custom ID generator  Any other key type, but best for numeric data types.

In approaches 2 and 3, DevForce will provide a temporary local key value for the new entity which will be replaced by a valid permanent value at the time the entity is saved to the database.  Neither approach is used in this sample, but you can read more about temporary IDs and ID fixup here.

View, Edit, Add, Delete

This example uses a simple DataForm to support view, edit and add capabilities.

Note that the only difference between Edit and View is that Edit displays the DataForm already in edit mode, so that the user can commence making changes immediately. When the selected entity is displayed with the DataForm in View mode it can still be edited, but the user must first click the “pencil” icon in the upper right corner of the DataForm to enter that mode.

Delete here forces a cascaded delete.  Here the customer's orders and the order details are also deleted, by first querying for them and then manually deleting them.  Another approach to this is to set up your model for cascaded deletes.  

Prerequisites

The Silverlight 4 Toolkit

Created by DevForce on July 13, 2010 18:15

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade