如何记录/审核插入/添加C#EF4的更改

时间:2018-10-11 05:31:11

标签: c# entity-framework-4

是否存在非复杂的审计实体更改以进行插入/添加的方法? 我使用下面的代码来记录实体更改以进行编辑/更新

var context = new DBEntities(connection());
try
{
    BPCategory Bp = context.BPCategories.First(e => e.id == id);
    Bp.Category = Category;
    Bp.PercentShare = Percent;
    Bp.BPCategory1 = BPCategory;
    Bp.Code = Code;
    Bp.Status = Inactive;


    var objState = context.ObjectStateManager.GetObjectStateEntry(Bp);
    var modProps = objState.GetModifiedProperties();
    foreach (var propName in modProps)
    {
        if (!Equals(objState.OriginalValues[propName], objState.CurrentValues[propName]))
        {
            //save audit
        }
    }
    context.SaveChanges();
}
catch (Exception ex)
{
    throw ex;
}

但是在添加/插入时我很挣扎。我该如何进行插入/添加?

var context = new DBEntities(connection());
BPCategory Bp = new BPCategory
{
    Category = Category,
    PercentShare = PercentShare,
    BPCategory1 = BPCategory,
    Code = Code,
    Status = Inactive
};
context.BPCategories.AddObject(Bp);
context.SaveChanges();

0 个答案:

没有答案