如何获取实体的旧值?

发布于 2024-12-12 08:14:41 字数 364 浏览 0 评论 0原文

如何获取实体的旧值?

按照这个例子..

public void Update(User user)
    ValidateEntity(user, OperationType.Update);

    oldUser = (how do I get the old values ​​(database) of the entity User?)

    Set.Attach(user);
    Context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
    Context.SaveChanges();

    OnUpdated(user, oldUser);
}

How do I get the old values of an entity?

follows the example..

public void Update(User user)
    ValidateEntity(user, OperationType.Update);

    oldUser = (how do I get the old values ​​(database) of the entity User?)

    Set.Attach(user);
    Context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
    Context.SaveChanges();

    OnUpdated(user, oldUser);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤芳又自赏 2024-12-19 08:14:41

试试这个:

public void Update(User user)
    ValidateEntity(user, OperationType.Update);

    var oldUser = Set.Single(u => u.Id == user.Id);
    Context.Detach(oldUser);

    Set.Attach(user);
    Context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
    Context.SaveChanges();

    OnUpdated(user, oldUser);
}

或者这个:

public void Update(User user) 
{
    ValidateEntity(user, OperationType.Update);

    var oldUser = Set.Single(u => u.Id == user.Id);
    Set.ApplyCurrentValues(user);
    Context.SaveChanges(SaveOptions.DetectChangesBeforeSave);

    OnUpdated(user, Context.ObjectStateManager.GetOjectStateEntry(user).OriginalValues);

    Context.AcceptAllChanges(); 
}

Try this:

public void Update(User user)
    ValidateEntity(user, OperationType.Update);

    var oldUser = Set.Single(u => u.Id == user.Id);
    Context.Detach(oldUser);

    Set.Attach(user);
    Context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
    Context.SaveChanges();

    OnUpdated(user, oldUser);
}

Or this:

public void Update(User user) 
{
    ValidateEntity(user, OperationType.Update);

    var oldUser = Set.Single(u => u.Id == user.Id);
    Set.ApplyCurrentValues(user);
    Context.SaveChanges(SaveOptions.DetectChangesBeforeSave);

    OnUpdated(user, Context.ObjectStateManager.GetOjectStateEntry(user).OriginalValues);

    Context.AcceptAllChanges(); 
}
汹涌人海 2024-12-19 08:14:41

我找到了一种使用反射将 DbDataRecord 转换为实体类型的方法...

其中 http ://www.instanceofanobject.com/2011/01/ef4-dbdatarecord-convertto.html

    public static class AnonymousTypeConversion
    {
        /// 
        /// Converts a single DbDataRwcord object into something else.
        /// The destination type must have a default constructor.
        /// 
        /// 
        /// 
        /// 
        public static T ConvertTo(this DbDataRecord record)
        {
            T item = Activator.CreateInstance();
            for (int f = 0; f 
        /// Converts a list of DbDataRecord to a list of something else.
        /// 
        /// 
        /// 
        /// 
        public static List ConvertTo(this List list)
        {
            List result = (List)Activator.CreateInstance>();

            list.ForEach(rec =>
            {
                result.Add(rec.ConvertTo());
            });

            return result;
        }
    }

I found one way of convert DbDataRecord to entity type using reflection...

where http://www.instanceofanobject.com/2011/01/ef4-dbdatarecord-convertto.html

    public static class AnonymousTypeConversion
    {
        /// 
        /// Converts a single DbDataRwcord object into something else.
        /// The destination type must have a default constructor.
        /// 
        /// 
        /// 
        /// 
        public static T ConvertTo(this DbDataRecord record)
        {
            T item = Activator.CreateInstance();
            for (int f = 0; f 
        /// Converts a list of DbDataRecord to a list of something else.
        /// 
        /// 
        /// 
        /// 
        public static List ConvertTo(this List list)
        {
            List result = (List)Activator.CreateInstance>();

            list.ForEach(rec =>
            {
                result.Add(rec.ConvertTo());
            });

            return result;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文