What is the optimal way to remove an high number of objects ?
With Session.Remove() is entity data fetched in memory before removal? (probably a dumb question...)
Session.Remove(Query.All<MyEntity>().Where(e => e.ToRemove == true)):
Is there a more optimal way?
ReferentialIntegrityException during Remove
Is this a bug? Or should specify a special OnOwnerRemove action on my EntitySet so that when a "MyEntity" instance is removed all the links from and to this instance are removed too?
[HierarchyRoot]
public class MyEntity : Entity
{
[Field, Key]
public int Id { get; private set; }
[Field, Association(PairTo = "Source")]
public EntitySet<Link> LinksFromThis { get; private set; }
[Field, Association(PairTo = "Destination")]
public EntitySet<Link> LinksToThis { get; private set; }
}
[HierarchyRoot]
public class Link : Entity
{
[Field, Key]
public int Id { get; private set; }
[Field]
public MyEntity Source { get; set; }
[Field]
public MyEntity Destination { get; set; }
}
using (Session.Open(domain))
{
using (var transactionScope = Transaction.Open())
{
// Creating new persistent object
var myEntity1 = new MyEntity();
var myEntity2 = new MyEntity();
var link = new Link() { Source = myEntity1, Destination = myEntity2 };
// Committing transaction
transactionScope.Complete();
}
}
// Reading all persisted objects from another Session
using (Session session = Session.Open(domain))
{
using (var transactionScope = Transaction.Open())
{
session.Remove(Query.All<MyEntity>().Take(1));
transactionScope.Complete();
}
}
Xtensive.Storage.ReferentialIntegrityException was unhandled
Message=Referential integrity violation on attempt to remove 'TestDoRemove.Model.MyEntity', Key='MyEntity, (1)'.
Source=Xtensive.Storage
StackTrace:
at Xtensive.Storage.ReferentialIntegrity.DenyActionProcessor.Process(RemovalContext context, AssociationInfo association, Entity removingObject, Entity target, Entity referencingObject, Entity referencedObject)
at Xtensive.Storage.ReferentialIntegrity.RemovalProcessor.ProcessItems(IList`1 entities)
at Xtensive.Storage.ReferentialIntegrity.RemovalProcessor.Remove(IEnumerable`1 entities)
at Xtensive.Storage.Session.Remove[T](IEnumerable`1 entities)
at TestDoRemove.Program.Main(String[] args)
Updated at 17.06.2010 7:58:52
Yes you are right : after a short reflexion I found that my attributes for the EntitySets should be like this:
[HierarchyRoot]
public class MyEntity : Entity
{
[Field, Key]
public int Id { get; private set; }
[Field, Association(PairTo = "Source", OnOwnerRemove = OnRemoveAction.Cascade, OnTargetRemove = OnRemoveAction.Clear)]
public EntitySet<Link> LinksFromThis { get; private set; }
[Field, Association(PairTo = "Destination", OnOwnerRemove = OnRemoveAction.Cascade, OnTargetRemove = OnRemoveAction.Clear)]
public EntitySet<Link> LinksToThis { get; private set; }
}
This works like a charm : many thanks!
PS : maybe the exception message should be more explicit (but I don't know if that's feasible in all cases ;) )
This thread was imported from our support forum. The original discussion may contain more detailed answer.
asked
Jun 16 '10 at 10:41
olorin
358●87●87●92