A way to implement default values.

An easy and simple way to implement default values in Entity Framework is to override the SaveChanges Event from the datacontext object.

In order to override the SaveChanges event you have to create the additional partial class from you datacontext.

Now you can override the SaveChanges event.
In the overridden SaveCanges event you check if there is a new entry ( the other option is EntityState.Modified ).
Then check if the entry.Entity is of the Type you wich and add the default values for new records.

    public partial class CentraalArtikelenBestand : global::System.Data.Objects.ObjectContext
        public override int SaveChanges(System.Data.Objects.SaveOptions options)
            if (ObjectStateManager != null)
                // new entitys
                if (ObjectStateManager.GetObjectStateEntries(EntityState.Added).Count() > 0)
                    foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added))
                        if (entry.Entity is CentraalArtikelBestand)
                            CentraalArtikelBestand centraalArtikelBestand = (CentraalArtikelBestand)entry.Entity;
                            centraalArtikelBestand .DateModified = DateTime.Today;
                            centraalArtikelBestand .Status = 0;                        }





Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!