Monday, January 02, 2012

Lightswitch 2011 - Entity default creation date and time

Creation date and time
Entities have no default property. If the entity needs a creation datetime field that registers the time when an entity record was created you can use the ' <entity> _Created' event from the from the 'Write Code' drop-down menu in the entity designer. 
For instance for an entity called 'Personeel' I want to use a field DatumCreatie that contains the date and time when the record was created. In the '<entity>_Created' event I need to add this code:
        partial void Personeel_Created()
        {
            // set default values for any field of the entity where necessary
            this.DateCreation = DateTime.Now;
        }
DateCreation is set as a required field in the entity designer.
In Lightswitch screens I use the DateTime Viewer control to show this Creation datetime field, since it should not be modifiable by the user.
Modification date and time
To track each time a record has been changed, I have added another datetime field to the entity. In the 'Write Code' I have selected the '<entity>_Updating' event, to change the modification date.
        partial void Personeels_Updating(Personeel entity)
        {
            entity.DateModification = DateTime.Now;
        }
DateModification is not set as required in the entity designer.
Take care NOT to use the '<entity>_Updated' event. Because the change of the date would retrigger the same event endlessly (i.e. until you run into stack overflow).
Creation and modification datetime fields default values and triggers
Creation date can be managed in SQL server by specifying a default value for the DateCreation field. The default value is a sql server GETDATE() function.
For the modification date a trigger after update could be used.

2 comments:

Unknown said...

louis vuitton outlet, sac longchamp, ralph lauren pas cher, replica watches, louboutin outlet, louboutin shoes, christian louboutin outlet, prada outlet, ugg boots, longchamp pas cher, tiffany and co, louis vuitton, louboutin, air jordan pas cher, tory burch outlet, cheap oakley sunglasses, nike outlet, polo ralph lauren outlet, ray ban sunglasses, polo ralph lauren outlet, louis vuitton outlet, nike air max, replica watches, louis vuitton, air max, longchamp outlet, michael kors, oakley sunglasses, chanel handbags, nike free, nike roshe run, oakley sunglasses, burberry, tiffany jewelry, ray ban sunglasses, kate spade outlet, prada handbags, oakley sunglasses, nike air max, louis vuitton, louboutin pas cher, uggs on sale, ray ban sunglasses, oakley sunglasses, longchamp, jordan shoes, gucci outlet, ugg boots, nike free, longchamp outlet

Unknown said...

hollister, louis vuitton, canada goose outlet, moncler, pandora jewelry, swarovski, louis vuitton, bottes ugg, coach outlet, moncler, supra shoes, montre pas cher, moncler, ugg,uggs,uggs canada, ugg,ugg australia,ugg italia, canada goose, links of london, pandora jewelry, karen millen, doudoune canada goose, juicy couture outlet, moncler, pandora charms, marc jacobs, swarovski crystal, moncler, ugg pas cher, thomas sabo, louis vuitton, moncler outlet, moncler, canada goose, canada goose uk, canada goose outlet, ugg boots uk, juicy couture outlet, wedding dresses, moncler, canada goose, toms shoes, louis vuitton, replica watches, sac louis vuitton pas cher, pandora charms, canada goose