I use entity framework 6.1 with Code First approach. When I use DbContext.SaveChanges() as the result I can get
The number of objects written to the underlying database.
But is that possible to get advanced statistics: number of rows deleted, number of rows edited, number of rows added?
My supergoal is to get per-Entity staticstics.
Maybe before actually calling SaveChanges()..
var modifiedCount = dbContext.ChangeTracker.Entries().Where(x => x.State == System.Data.EntityState.Modified).Count()
The above line of code can be used to fetch the modified entries, and similarly you can fetch the deleted and added too..
But you have to execute these lines of code before you call SaveChanges().
You could use db.GetChangeSet()
. This will contains three lists:
var deletes = db.GetChangeSet().Deletes;
var updates = db.GetChangeSet().Updates;
var inserts = db.GetChangeSet().Inserts;
var all = db.GetChangeSet().All;
This can be done before you submit the changes to the database.
Considering having a table called country
db.Tblcountries.InsertOnSubmit(
new Tblcountry(){FkRegionId=1,CountryCode="US",Name="USA"});
var result= db.GetChangeSet().All.ToList();
The All
list and the Insert
list will contain this list:
PkCountryId Name CountryCode FkRegionId
0 USA US 1
Reference:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With