I'm getting an 'Entity is removed' error when calling a .Remove() method.

I think it's a good place to ignore this error.

System.InvalidOperationException: Entity is removed.
    Xtensive.Orm.Entity.EnsureNotRemoved()
    Xtensive.Orm.ReferentialIntegrity.RemovalProcessor.Remove(IEnumerable`1 entities)
    Xtensive.Orm.Entity.Remove()
    MyAssembly.Upgrader.OnUpgrade()
    Xtensive.Orm.Upgrade.UpgradeHandler.OnStage()
    Xtensive.Orm.Upgrade.UpgradingDomainBuilder.OnStage(Session session)
    Xtensive.Orm.Upgrade.UpgradingDomainBuilder.PerformUpgrade(Domain domain, UpgradeStage stage)
    Xtensive.Orm.Upgrade.UpgradingDomainBuilder.BuildMultistageDomain()
    Xtensive.Orm.Upgrade.UpgradingDomainBuilder.Run()
    Xtensive.Orm.Upgrade.UpgradingDomainBuilder.Build(DomainConfiguration configuration)

Model

[HierarchyRoot]
public class MyEntity: Entity
{
      [Field, Key]
      public long Id { get; private set; }

      [Field]
      [Association(OnOwnerRemove = OnRemoveAction.Cascade, OnTargetRemove = OnRemoveAction.Cascade)]
      public MyChild MyChild { get; private set; }
}

[HierarchyRoot]
public class MyChild: Entity
{
      [Field, Key]
      public long Id { get; private set; }
}

Code

myEntity.MyChild.Remove();
myEntity.Remove();

DataObjects.Net version : 4.6.4.1123

asked Jun 03 '13 at 03:24

olorin's gravatar image

olorin
356838489

edited Jun 07 '13 at 01:52


One Answer:

Hello olorin,

I think this is expected behavior since you have OnTargetRemove=Cascade.

We prefer not to change default behavior unless there are strong reasons. You could make a feature request for such option however.

answered Jun 06 '13 at 02:58

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov
179325

The scenario I'm reporting is: I have an object A of type MyEntity and an oject B of type MyChild. It is normal that object A removal triggers object A removal.

What I do not understand is that calling B.Remove(); A.Remove(); throws an exception. Is B is already removed and can not be removed a second time, is that an error?

(Jun 07 '13 at 01:58) olorin olorin's gravatar image

MyEntity type has OnTargetRemove=OnRemoveAction.Cascade setting. In other words when target of the association (MyChildEntity) is removed corresponding owner (MyEntity) is removed as well.

This means your first remove statement actually removes both entities because DataObjects.Net is instructed to do so. Your second remove statement tries to remove an entity that is already removed, which is not allowed and thus an exception is thrown.

(Jun 07 '13 at 02:51) Denis Krjuchkov Denis%20Krjuchkov's gravatar image

To sum up you're saying this is by design?

myEntity.Remove(); myEntity.Remove(); will throw an exception by design

(Jun 11 '13 at 10:22) olorin olorin's gravatar image

That's right

(Jun 13 '13 at 03:54) Denis Krjuchkov Denis%20Krjuchkov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×569
×7

Asked: Jun 03 '13 at 03:24

Seen: 21,899 times

Last updated: Jun 13 '13 at 03:54

powered by OSQA