locked
Dynamic Data using EF: How to set the datetime for Last Modified Date field? RRS feed

  • Question

  • User992715993 posted

    Hi, I'm using .NET 3.5.

    I create a formview with insert and update capability using EF model. I have multiple tables that have the same column called date_last_modified to capture both create and update dates. How do I set it to current datetime when inserting or updating without using database triggers?

    I have read these articles. http://csharpbits.notaclue.net/2008/11/dynamicdata-automatic-column-update-for.html AND http://msdn.microsoft.com/en-us/library/cc716714.aspx .

    private static void OnSavingChanges(object sender, EventArgs e)
            {

                // This will populate the PasswordHash and PasswordSalt fields
               
    var stateManager = ((NorthwindEntities)sender).ObjectStateManager;
                var insertedEntities = stateManager.GetObjectStateEntries(EntityState.Added);

                foreach (ObjectStateEntry stateEntryEntity in insertedEntities)
                {

    // IS THERE A WAY TO GENERICALLY TRAVERSE "stateEntryEntity.Entity" TO FIND A COMMON COLUMN (i.e. date_last_modified) ACROSS MULTIPLE TABLES/ENTITIES TO SET IT'S VALUE HERE BEFORE SAVING? 
                    if (stateEntryEntity.Entity is Customers)  {
                        Customers cust = (Customers)stateEntryEntity.Entity;
                        cust.CreatedBy = HttpContext.Current.User.Identity.Name;
                        cust.CreatedOn = DateTime.Now;
                        cust.UpdatedBy = HttpContext.Current.User.Identity.Name;
                        cust.UpdatedOn = DateTime.Now;
                    }
                }

    Thanks in advance for your help!

    Thursday, June 9, 2011 4:34 PM

Answers

  • User992715993 posted

    Topolov,

    I finally figured it out with the help of this post and it seems to fulfill my purpose. http://forums.asp.net/t/1441201.aspx/1?How+to+set+current+DateTime+Today+as+default+field+value+in+edit+insert+mode

    // Set last_modified_by and last_modified_date

    var stateManager = ((PersonalEntities)sender).ObjectStateManager;

    var changedEntities = stateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added);

    foreach (ObjectStateEntry stateEntryEntity inchangedEntities)

    {

    if(!stateEntryEntity.IsRelationship){

    int modBy = stateEntryEntity.CurrentValues.GetOrdinal("last_modified_by");

    int modDate = stateEntryEntity.CurrentValues.GetOrdinal("date_last_modified");

    if(modBy > 0)

    {

    stateEntryEntity.CurrentValues.SetValue(modBy,

    HttpContext.Current.User.Identity.Name);

    }

    if (modDate > 0)

    {

    stateEntryEntity.CurrentValues.SetValue(modDate,

    DateTime.Now);

    }

    }

    }

    }

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 9, 2011 9:45 PM

All replies

  • User1043796333 posted

    I'd lke to know how to do this too

    Thursday, June 9, 2011 4:57 PM
  • User992715993 posted

    Topolov,

    I finally figured it out with the help of this post and it seems to fulfill my purpose. http://forums.asp.net/t/1441201.aspx/1?How+to+set+current+DateTime+Today+as+default+field+value+in+edit+insert+mode

    // Set last_modified_by and last_modified_date

    var stateManager = ((PersonalEntities)sender).ObjectStateManager;

    var changedEntities = stateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added);

    foreach (ObjectStateEntry stateEntryEntity inchangedEntities)

    {

    if(!stateEntryEntity.IsRelationship){

    int modBy = stateEntryEntity.CurrentValues.GetOrdinal("last_modified_by");

    int modDate = stateEntryEntity.CurrentValues.GetOrdinal("date_last_modified");

    if(modBy > 0)

    {

    stateEntryEntity.CurrentValues.SetValue(modBy,

    HttpContext.Current.User.Identity.Name);

    }

    if (modDate > 0)

    {

    stateEntryEntity.CurrentValues.SetValue(modDate,

    DateTime.Now);

    }

    }

    }

    }

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 9, 2011 9:45 PM