0

I am updating data in the table and then on its update inserting date in other history table with has same fields as first table.

my code is:

 public void SaveChanges()
    {
      objectdateContext.SaveChanges();

      saveInHistoryTable(); // here how i can pass the current entity.
    }

 public void saveInHistoryTable(FirstTable f)
 {
    SecondTable s = new SecondTable();
    s.id = f.id;
    s.Name = f.Name;
    objectdateContext.Insert(s);
}

Please guide me thanks.

4

2 回答 2

0

您可以在上下文中重载 SaveChanges(),然后为 ChangeTracker 中的每个对象进行保存。

这是我之前使用过的一个示例(这是针对 DbContext,如果您有一个对象上下文,它会略有不同):

  public override int SaveChanges()
  {
     if( ChangeTracker.Entries().Any( e => e.State != EntityState.Unchanged ) )
     {
        //insert the entry into the History table
     }

     return base.SaveChanges();
  }
于 2012-10-14T21:25:26.753 回答
0

saveInHistoryTable(); // here how i can pass the current entity.

The entity is long gone by this point. You should be calling this method and passing in the entity after the changes are made in whatever method that occurs at.

You should separate out these concerns into different methods. Honestly, SaveChanges() should not include making a backup of a "current entity". Moreover, how do you know that only one entity was changed? If it were an object graph that were changed, then it would be impossible to only pass in one object and have the whole graph backed up.

于 2012-10-14T21:10:25.110 回答