简体   繁体   English

自动删除孤立的孩子:第二次提交将不会删除孩子

[英]Automatically delete orphaned childs: Childs will not be deleted for the second Commit

Further to question Can EF automatically delete data that is orphaned, where the parent is not deleted? 进一步质疑EF是否可以自动删除孤立的数据,而不删除父数据?

I checked the solution, as @Ladislav suggested here https://stackoverflow.com/a/10838228/54801 Unfortunately I noticed that the solution works partially. 我检查了解决方案,就像@Ladislav在这里建议的那样https://stackoverflow.com/a/10838228/54801 不幸的是,我注意到该解决方案部分起作用。

If we take the following code: 如果我们采用以下代码:

 for (int k = 0; k < 2; k++)
 {
     var parentObject= _context.Parents.Get(1704);
     parentObject.ChildObjects.Clear();

     var childObject= _context.ChildObjects.Get(1000*k /*diffrent child for each iteration*/ );
     parentObject.ChildObjects.Add(childObject);

     _context.Commit();
  }

The first iteration (k=0) was carried out deletion. 进行第一次迭代(k = 0)。 But the second iteration will not delete! 但是第二次迭代不会删除!

Does anyone have a solution to the problem? 有谁能解决这个问题?

The problem can be "solved" as shown here: 可以如下所示“解决”问题:

if(k == 1)
{
    foreach (var project in tender.TenderProjects)
      {
         _context.Projects.Remove(project);
      }
   }

But it's definitely not ideal I want my services out of any ORM logic.. 但是,我希望我的服务脱离任何ORM逻辑绝对不理想。

EDIT: 编辑:

The Iteration is just an example of the real scenario. 迭代只是真实场景的一个示例。 Real scenario, the user can add the parent entity's first child. 在实际情况下,用户可以添加父实体的第一个孩子。 save the data and go again to remove the first child and add two other childs in his place 保存数据,然后再次删除第一个孩子,并在其位置添加另外两个孩子

It turns out that the problem was pretty stupid. 事实证明,这个问题非常愚蠢。 My entities inherit from base class that implaments the methods Equals and GetHashCode base on unique Id. 我的实体从基类继承,该基类基于唯一的ID嵌入方法Equals和GetHashCode。

    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is Entity))
            return false;

        if (Object.ReferenceEquals(this, obj))
            return true;

        Entity item = (Entity)obj;

        if (item.IsTransient() || this.IsTransient())
            return false;
        else
            return item.Id == this.Id;
    }


    public override int GetHashCode()
    {
        if (!IsTransient())
        {
            if (!_requestedHashCode.HasValue)
                _requestedHashCode = this.Id.GetHashCode() ^ 31;

            return _requestedHashCode.Value;
        }
        else
            return base.GetHashCode();

    }

Because of my entity has a composite key. 由于我的实体具有复合键。 I need to override that methods relates to the composite key. 我需要重写与组合键相关的方法。

        public override bool Equals(object obj)
    {
        if (obj == null || !(obj is ProjectContract))
            return false;

        if (Object.ReferenceEquals(this, obj))
            return true;

        var item = (ProjectContract)obj;

        if (item.IsTransient() || this.IsTransient())
            return false;

        return item.Id == this.Id && item.ProjectId == this.ProjectId;
    }

    int? _requestedHashCode;
    public override int GetHashCode()
    {
        if (!_requestedHashCode.HasValue)
            _requestedHashCode = (this.Id.ToString() + this.ProjectId.ToString()).GetHashCode();

        return _requestedHashCode.Value;
    }

Thanks for your attention and desire to help 感谢您的关注和帮助的渴望

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

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