EF字段更新问题 [英] EF Field Update Problem

查看:98
本文介绍了EF字段更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    

Hi,
    

    我有一个简单的DTO对象,我填充,转换为EF实体对象并更新数据库。 这似乎工作正常。 我的问题在于:

     I have a simple DTO object which I populate, convert to an EF entity object and update a database.  This, seems to work properly.  My problem lies in the following:

    我有一个SQL表,其中包含一个名为VisitCount的字段(类型为int)。 执行更新过程时,所有内容似乎都有效,但VisitCount值更新不正确。

     I have an SQL table with a field called VisitCount (of type int).  When performing the update process, all seems to work except that the VisitCount value is updated incorrectly.

    我在我的  WebService中使用以下内容:

     I use the following in my WebService:

     DTOCounter.VisitCount + = X;

     DTOCounter.VisitCount += X;

    当我将DTOCounter对象传递给我的DAL并将其转换为EF实体时,保存到数据库的VisitCount值不会考虑现有值。 当我使用+ =运算符时,我认为这是隐含的。 数据库仅包含我通过的最后一个VisitCount值,并忽略+ = X运算符。

     When I pass the DTOCounter object to my DAL and convert it to an EF entity, the VisitCount value saved to the database does not take into consideration, the existing value.  I thought this was implicit when I used the += operator.  The database only contains that last VisitCount value that I passed and ignores the += X operator.

    我正在使用context.ApplyPropertyChanges,但它仍然不起作用。 能帮忙吗?

     I am using the context.ApplyPropertyChanges but it still doesn't work.  Can you please help?

    本质上,我需要在数据库中添加当前的VisitCount字段值,并使用我在DTO中传递的值(然后将其转换为EF对象)

     In essence, I need to add to the current VisitCount field value in the database with a value which I pass in my DTO (which then gets converted into an EF object)

     这可以使用context.ApplyPropertyChanges方法自动完成,还是必须手动执行这些操作? 如果是这样,我在哪个层执行这些操作?

     Can this be done automatically using the context.ApplyPropertyChanges method or do I have to perform these operations manually?  If so, in which layer do I perform these operations in?

    感谢您提供任何帮助。

     Thank you for any and all help.


    最诚挚的问候,


     Best Regards,

          Giovanni Palmiotto

          Giovanni Palmiotto

推荐答案

因此,如果我理解正确,您希望从数据库中获取VisitCount的值,添加到它,然后将结果保存到数据库? 您可以通过查询当前实体,执行添加(以及进行您需要的
的任何其他更改),然后调用SaveChanges来执行此操作。 但是,您需要注意并发性,因为如果同时发生另一个查询/更新/保存周期,那么数据库中的结果将是错误的。 您可以通过并发
令牌(例如时间戳或甚至可能是VisitCount属性本身)来管理它,然后处理保存时出现并发异常的情况。

So if I understand correctly, you want to get the value of VisitCount from the database, add to it, and then save the result to the database?  You can do this by querying for the the current entity, performing the add (and making any other changes that you need), and then calling SaveChanges.  However, you will need to be careful about concurrency since if another query/update/save cycle happens at the same time then the result in the database will be wrong.  You can manage this by having a concurrency token (e.g. a timestamp or maybe even the VisitCount property itself) and then handling the case where you get a concurrency exception on save.

希望这会有所帮助,

Arthur


这篇关于EF字段更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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