This code samples presents a small Silverlight application with simple data editing capabilities to add, edit and delete Customer entities.
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?
This solution builds upon a simple query explorer type of interface to provide simple editing capabilites for Customer entities.
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 Value | When Appropriate | |
---|---|---|
1 | Set it directly | Key is a GUID |
2 | 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. |
3 | Invoke 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.
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.