简体   繁体   English

带有UoW更新的EF存储库

[英]EF Repository with UoW Update

I believe this is asked somewhere else but I can't find straight solution. 我相信这是在其他地方提出来的,但我找不到直接的解决方案。 My Api is passing object model and on the server side every value of that object which is not passed is considered null (makes sense). 我的Api正在传递对象模型,并且在服务器端,该未传递对象的每个值都被视为null(有意义)。 Is there way I can tell EF6 not to update entity with null values from passed object in manner I don't have to write each property and check if it's null. 有没有办法我可以告诉EF6不要用传递的对象中的空值更新实体,而不必编写每个属性并检查它是否为空。

Pseudo code 伪代码

API API

Update(int id, TaskEntity obj)
{
    unitOfWork.Tasks.Update(id, userTask);
    ...
    unitOfWork.Save()
}

Repo update 回购更新

Update(int id, T entity)
        {
            var existingRecord = Get(id); //Gets entity from db based on passed id
            if (existingRecord != null)
            {
                var attachedEntry = Context.Entry(existingRecord);
                attachedEntry.CurrentValues.SetValues(entity);
            }
        }

My problem is that any data with null values will actually rewrite existing db record value with nulls. 我的问题是,具有空值的任何数据实际上都会重写具有空值的现有数据库记录值。

Please point me to a solution or article where this is solved. 请指出解决方案或文章。 Should I go reflections, maybe automapper could handle this (it's not its purpose i believe), or some kind of helper method should be written, as my objects can contain sub object. 我应该反思一下,也许自动映射器可以解决这个问题(我相信这不是它的目的),或者应该编写某种辅助方法,因为我的对象可以包含子对象。

Thank you in advance. 先感谢您。

You can do something like this 你可以做这样的事情

Update(int id, T entity,string[] excludedFields)
{
    var existingRecord = Get(id); //Gets entity from db based on passed id
     if (existingRecord != null)
     {
          var attachedEntry = Context.Entry(existingRecord);
          attachedEntry.CurrentValues.SetValues(entity);
          for(var field in excludedFields)
          {
               attachedEntry.Property(field).IsModified = false;
          }
     }
}

some scenaries requires you to update part of the object and sometimes other parts, the best way in my opinion is to pass the fields to exclude from the update 有些场景需要您更新对象的一部分,有时需要更新其他部分,我认为最好的方法是传递字段以将其排除在更新之外

hope it will help you 希望对你有帮助

Personally not a big fan of hitting database and doing a get operation before doing an update. 就个人而言,不喜欢在更新之前先打数据库并执行get操作。 May be while doing the ajax call, you can send a list of properties which you should update (so that the scenario where updating to null values (erasing existing ones) will also be handled). 可能在进行ajax调用时,您可以发送应更新的属性列表(以便也可以处理更新为null值(擦除现有值)的情况)。

I'm doing small modifications to what @Hadi Hassan has done (without hitting database for getting the entity): 我正在对@Hadi Hassan所做的事情做一些小修改(无需访问数据库即可获取实体):

Update(T entity,string[] includedFields)
{
    var existingRecord = Context.Attach(entity); // assuming primary key (id) will be there in this entity
    var attachedEntry = Context.Entry(existingRecord);

    for(var field in includedFields)
    {
       attachedEntry.Property(field).IsModified = true;
    }

}

Note - attachedEntry.Property(field).IsModified will not work for related entities 注意-AttachedEntry.Property(field).IsModified不适用于相关实体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM