0

是否可以使用实体框架为数据库中要添加的行保留 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.
}
4

1 回答 1

1

您不能要求数据库预先分配 id。你有2个选择

  1. 实现你自己的身份服务器
  2. 数据写入数据库后获取id

这是选项 2 的几个示例。它们都做同样的事情 - 在调用之前构建添加项目的列表SaveChanges()并在之后迭代它们。

public interface IIDentifiable
{
    int id { get; set; }
}

public override int SaveChanges()
{
    ChangeTracker.DetectChanges();
    List<IIDentifiable> addedObjects = GetNewEntries();
    int result = base.SaveChanges();
    foreach (IIDentifiable o in addedObjects)
    {
        //log o.id
    }
    return result;
}

private List<IIDentifiable> GetNewEntries()
{
    var result = (
        from e in ChangeTracker.Entries()
        where e.State == System.Data.EntityState.Added
        select e.Entity)
        .OfType<IIDentifiable>()
        .ToList();

    return result;
}

的缺点IIDentifiable是获取其他详细信息,例如Type等。

第二种选择是做同样的事情,但使用dynamic而不是IIDentifiable

public override int SaveChanges()
{
    ChangeTracker.DetectChanges();
    List<dynamic> addedObjects = GetNewEntries();
    int result = base.SaveChanges();
    foreach (dynamic o in addedObjects)
    {
        //log o.id
        //log o.GetType()
    }
    return result;
}

private List<dynamic> GetNewEntries()
{
    var result = (
        from e in ChangeTracker.Entries()
        where e.State == System.Data.EntityState.Added
        select e.Entity)
        .ToList();

    return result;
}
于 2013-06-04T12:46:29.867 回答