Get all tracked entities from a DbContext?

前端 未结 3 1395
离开以前
离开以前 2021-02-04 02:50

Many of the tables in my database need to have a \"DateCreated\" and \"DateModified\" column. I want to update these columns whenever SaveChanges() is called.

3条回答
  •  不知归路
    2021-02-04 03:48

    The accepted solution here is better for above EF4 than the ObjectContext suggestion - we ran into issues with for instance the in memory db we used in the test context not working well with the above solution (getting Detached state for some modified entries in inheritance scenarios). This solution uses the DbContext native way of retrieving the tracked entities:

    context.ChangeTracker.Entries()
            .Where (t => t.State == EntityState.Modified)
    

提交回复
热议问题