• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Blog
  • Power Platform
    • Power Apps
    • Power Automate
    • Power BI
    • Power Virtual Agents
  • Dynamics 365
  • Azure
  • About Me
Joe Gill Logo

Joe Gill

Microsoft MVP - Power Platform Consultant

Home » .NET » Auto update columns in Dynamic Data

Auto update columns in Dynamic Data

7th September 2009 by Joe Gill Leave a Comment

Lots of people have a requirement to have some columns automatically updated when using Dynamic Data in conjunction with the Entity Framework. These are typically audit type columns such as the ModifiedDate column found in the AdventureWorks database.

The typical solution is to create a handler to trap the event so that you can change the data before it is saved to the database.

public partial class AdventureWorksEntities : global::System.Data.Objects.ObjectContext
    {
        partial void OnContextCreated()
        {
            this.SavingChanges += new EventHandler(OnSavingChanges);
 
        }
        private static void OnSavingChanges(object sender, EventArgs e)
        {
            foreach (ObjectStateEntry entry in ((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(
                 EntityState.Added | EntityState.Modified))
            {
                int i = entry.CurrentValues.GetOrdinal("ModifiedDate");
                entry.CurrentValues.SetValue(i, DateTime.Now);
            }

This can give you problems if you want a generic version for all tables but not all the tables have the ModifiedDate column and so GetOrdinal throws an exception. An alternative is to use some reflection to check if the Property exists and update accordingly and avoids exceptions being thrown.

foreach (ObjectStateEntry entry in ((ObjectContext)sender).ObjectStateManager.GetObjectStateEntries(
                EntityState.Added | EntityState.Modified))
            {
                // Ignore relationships
                if (!entry.IsRelationship)
                {
                    foreach (PropertyInfo pi in entry.Entity.GetType().GetProperties())
                    {
                        if (pi.Name == "ModifiedDate")
                            pi.SetValue(entry.Entity, DateTime.Now, null);
                    }
                }
            }

Share This On Social:
  • Tweet
  • Auto update columns in Dynamic Data Joe Gill Dynamics 365 Consultant

Filed Under: .NET

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Popular Categories

  • Power Platform
    • Power Apps
    • Power Automate
    • Power Virtual Agents
  • Azure
    • Logic Apps
  • Dynamics 365
  • .NET
  • AI
  • SQL

More to See

Power Platform Requests – Base Request Capacity

17th December 2020 By Joe Gill

Power Automate Desktop

Power Automate Desktop – UI Flow

26th November 2020 By Joe Gill

Tweets

Footer

Joe Gill

Microsoft Business Applications MVP – Power Platform, Dynamics 365 and Azure.

An architect with over twenty years experience designing and developing technology solutions. Specializing in the Microsoft technology stack including Power Platform, Dynamics 365 and Azure. Microsoft MVP Profile

Connect on Social

Useful Links

  • Home
  • Blog
  • About Joe Gill
  • Power Platform
  • Dynamics 365
  • Azure

Featured Posts

Power Platform Requests – Base Request Capacity

Power Automate Desktop – UI Flow

MB-600 – Solution Architect – Supportability

AI Builder – Form Processing Layouts

© 2021 · Joe Gill