【发布时间】:2013-06-04 12:31:38
【问题描述】:
是否可以使用实体框架为数据库中要添加的行保留 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.
}
【问题讨论】:
标签: sql-server sql-server-2008 entity-framework entity-framework-4 ado.net