none
How to update Modified date on update of foreign key Id in base table RRS feed

  • Question

  • I have Employee table and it has department_id of Department table.

    When I update the firstName and LastName fields of employee table modifiedDate is getting properly updated.

    But when I change department for employee, my modifiedDate is not getting changed. However, department_id is getting changed.

    Kindly let me know what wrong steps I am following.

    In my update method I am assigning employee.Department object with changed values as below:

    employee.Department = newly modified object of Departmentclass.

    employee.savechanges();

    Note: I need answer for Entity Framework's database first approach.



    • Edited by Ninds001 Monday, February 1, 2016 1:19 PM
    Monday, February 1, 2016 1:12 PM

Answers

  • Hi Ninds001,

    According to your description, I assume you want to modify the field “modifiedDate” when you change any properties in employee class. I suggest you manually change the date when you update the entity. Code snippet looks like the following.

    using (var db = new SchoolEntities())
    
    {
    
                    var emp = (from e in db.Employee_
    
                               where e.Id == 1
    
                               select e).FirstOrDefault();
    
                    var dep = (from d in db.Departments
    
                               where d.DepartmentID == 1
    
                               select d).FirstOrDefault();
    
                    if (emp != null)
    
                    {
    
                        emp.ModifyDate = DateTime.Now;
    
                        emp.Department = dep;
    
                        dep.Name = "NewName";
    
                        dep.StartDate = DateTime.Now;
    
                        db.Entry(emp).State = System.Data.Entity.EntityState.Modified;
    
                        db.SaveChanges();
    
                    }
    
                }
    

    Best regards,

    Cole Wu

    Tuesday, February 2, 2016 5:33 AM
    Moderator

All replies

  • I have Employee table and it has department_id of Department table.

    When I update the firstName and LastName fields of employee table modifiedDate is getting properly updated.

    But when I change department for employee, my modifiedDate is not getting changed. However, department_id is getting changed.

    Kindly let me know what wrong steps I am following.

    In my update method I am assigning employee.Department object with changed values as below:

    employee.Department = newly modified object of Departmentclass.

    employee.savechanges();

    Note: I need answer for Entity Framework's database first approach.



    Well create an update SQL Server trigger that will set the date and time modifiedDate when record is updated, or you set the date and time in the modefiedDate property when you do the update and save

    of the object.th

    Monday, February 1, 2016 4:04 PM
  • Hi Ninds001,

    According to your description, I assume you want to modify the field “modifiedDate” when you change any properties in employee class. I suggest you manually change the date when you update the entity. Code snippet looks like the following.

    using (var db = new SchoolEntities())
    
    {
    
                    var emp = (from e in db.Employee_
    
                               where e.Id == 1
    
                               select e).FirstOrDefault();
    
                    var dep = (from d in db.Departments
    
                               where d.DepartmentID == 1
    
                               select d).FirstOrDefault();
    
                    if (emp != null)
    
                    {
    
                        emp.ModifyDate = DateTime.Now;
    
                        emp.Department = dep;
    
                        dep.Name = "NewName";
    
                        dep.StartDate = DateTime.Now;
    
                        db.Entry(emp).State = System.Data.Entity.EntityState.Modified;
    
                        db.SaveChanges();
    
                    }
    
                }
    

    Best regards,

    Cole Wu

    Tuesday, February 2, 2016 5:33 AM
    Moderator