实体框架-在SavinChanges事件中获取身份值 [英] Entity Framework - Get identity value in SavinChanges event

查看:61
本文介绍了实体框架-在SavinChanges事件中获取身份值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我想知道是否有任何方法可以找出EntityObject在SavingChanges事件中获得的标识值.

任何帮助,感激不尽,
欢呼
Andy

Hi all,
I was wondering if there is any way to find out what identity value an EntityObject will get in the SavingChanges event.

any help is kindly appreciated,
cheers
Andy

推荐答案

就使用最新版本的EF(4.0,4.1)而言,获取最后插入的实体对象标识值没有问题. 注意仅在成功执行插入的语句后,才会返回标识列的值.

1.插入实体对象.
2.使用数据库上下文对象SaveChanges()方法保存实体对象
3.不要处理数据库上下文对象.
4.从您的方法返回实体对象.

As far as you are using the latest version for EF(4.0,4.1), there is no problem getting the last inserted entity object identity value. Note The identity column value will be returned only after the inserted statement committed successfully.

1. Insert the Entity Object.
2. Save the Entity Object using database context object SaveChanges() method
3. Don''t dispose the database context object.
4. Return the Entity Object from your method.

/// <summary>
/// Add Type Customer entity to the repository
/// </summary>
/// <typeparam name="Customer">Type Customer entity to be added</typeparam>
/// <param name="entity">Customer  entity to be added and returned</param>
/// <returns>Type Customer  entity object</returns>
/// <remarks></remarks>
public Customer  AddRecord<Customer>(Customer entity)
{
    try {
        bool success = entityRepository.AddRecord<Customer>(entity); // Note : The repository must not dispose the database context in order to get the last identity value of the Entity Object.
        if(success)
          return entity;
    } catch (OptimisticConcurrencyException concurrencyException) {
        entityRepository.Dispose();
        throw concurrencyException;
    } catch (Exception addRecordException) {
        entityRepository.Dispose();
        throw addRecordException;
    }
    return null;
}


存储库中的AddRecord 方法如下所示. 注意.它只是代码片段,存储库中应包含许多方法.


The AddRecord method from the repository looks something like this. Note. It just code fragment, there are lots of methods that should be contained in the repository.

/// <summary>
/// Add entity to the repository
/// </summary>
/// <typeparam name="E">An Entity type object type which is going to be added</typeparam>
/// <param name="entity">The entity to be added</param>
/// <returns>True/False</returns>
/// <remarks></remarks>
public bool AddRecord<E>(E entity)
{
    this.objectContext.AddObject(GetBaseType(typeof(E)).Name.ToString(), entity);
    this.objectContext.SaveChanges();
    return true;
}


在将行实际插入表中之前,身份值不存在.我本人并没有使用过EF,但是如果它遵循Microsoft标准,则在该事件发生之前将触发"SavingChanges",以允许您在实际保存该对象之前对其进行验证或修改.这意味着在这种情况下,数据库中将不会创建任何行,因此从逻辑上讲不可能获得标识值(因为数据库引擎尚未创建该值).

您需要保存一个占位符,并捕获成功保存行后发出的事件(猜测为"SavedChanges").此时,您应该能够确定实际分配的值.
Identity values don''t exist until the row is actually inserted into the table. I''ve not used EF myself but if it follows the Microsoft standards then ''SavingChanges'' will be fired before that happens, to allow you to validate or modify the object before it actually gets saved. That means that in this event, there won''t yet be a row created in the database and therefore it is logically impossible to get the identity value (since the database engine didn''t make one yet).

You need to save a placeholder, and catch the event issued when a row is successfully saved (''SavedChanges'' at a guess). At that point you should be able to determine the value that was actually assigned.


这篇关于实体框架-在SavinChanges事件中获取身份值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆