Entity Frameworkを使用してデータベースに追加される行のIDENTITY値を予約することは可能ですか?
の私のオーバーライドではObjectContext.SaveChanges
、次のようなことをしたい:
public override int SaveChanges(SaveOptions options)
{
var affectedEntities = ObjectStateManager.GetObjectStateEntries(
EntityState.Added |
EntityState.Modified |
EntityState.Deleted);
foreach (var entry in affectedEntities)
{
var type = GetObjectType(entry.Entity.GetType());
if (entry.State == EntityState.Added)
{
// log the creation of the new entity into a log file
// or a different table. For this, I need the newly
// created entity's ID. Is it possible?
}
}
return base.SaveChanges(options);
// if I check for ObjectState here, i.e. after SaveChanges,
// it won't work? Because once the changes are committed to the database
// the newly added object's state would no longer remain
// Added.
}