1

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

Community
  • 1
  • 1
ari
  • 331
  • 1
  • 4
  • 19
  • In the second iteration parent 1704 is already related to child 27788. Removing and then adding that child gets you back where you started, so there should be nothing to do when you call SaveChanges. – Arthur Vickers Oct 15 '12 at 16:30
  • 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 – ari Oct 16 '12 at 18:48
  • Can you provide a simple model and code that repros the problem? – Arthur Vickers Oct 16 '12 at 20:15

1 Answers1

1

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

ari
  • 331
  • 1
  • 4
  • 19