简体   繁体   中英

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?

I checked the solution, as @Ladislav suggested here https://stackoverflow.com/a/10838228/54801 Unfortunately I noticed that the solution works partially.

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. 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..

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.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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