Deleting from an EntityView

Posts   
 
    
Rono
User
Posts: 12
Joined: 04-Jan-2007
# Posted on: 08-Feb-2007 15:45:21   

I have a .NET 2.0 app using version 2.0.0.61120. It has a grid that's bound to an EntityView which I use to filter a collection. Whenever I try to delete a record the collection I get an exception. Help on how to get around this would be appreciated. Here is my code that creates the EntityView


    public JobAttorneyCollection CoCounselAttorneys
    {
      get
      {
        EntityView<JobAttorneyEntity> attorneyView = new EntityView<JobAttorneyEntity>(this.JobAttorneys, JobAttorneyFields.AttorneyTypeId == 3);
        return (JobAttorneyCollection)attorneyView.ToEntityCollection();
      }
    }

This is my code to delete a record from this collection:


  JobAttorneyEntity coCouncel = (JobAttorneyEntity)coCounselBindingSource.Current;
  _subject.CurrentJob.JobAttorneys.Remove(coCouncel);
  coCouncel.Delete(); 

When I delete a record from this EntityView I get this I get this exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> SD.LLBLGen.Pro.ORMSupportClasses.ORMEntityIsDeletedException: This entity is deleted from the database and can't be used in logic. at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.GetCurrentFieldValue(Int32 fieldIndex) at LCS.CaseManager.BLL.EntityClasses.JobAttorneyEntityBase.get_CounselTypeId() in C:\Projects\Legal Copy Services\CaseManager\LCS.CaseManager.BLL\EntityBaseClasses\JobAttorneyEntityBase.cs:line 1412

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 08-Feb-2007 16:40:57   

Reverse the sequence (and perhaps test for a successful deletion)


JobAttorneyEntity coCouncel = (JobAttorneyEntity)coCounselBindingSource.Current;
if (coCouncel.Delete())
{
 _subject.CurrentJob.JobAttorneys.Remove(coCouncel);
}

Rono
User
Posts: 12
Joined: 04-Jan-2007
# Posted on: 08-Feb-2007 16:52:35   

Thanks, but your suggestion did not fix the problem.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 09-Feb-2007 04:58:27   

Is this exception being thrown by access to this.JobsAttorney? This usually occurs when you delete an entity from the db and then try to access it in the a collection. Something similar to this post possibly.

http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7593&HighLight=1

If this isn't the case at all which line exactly is throwing the error?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 09-Feb-2007 12:27:18   

If you call ToEntitycollection, the entity object itself is the same, the new entitycollection gets a reference to the SAME entities. So the entity is also in the ORIGINAL collection, which has a view on it.

The call to Delete on the entity in the second collection doesn't remove it from the first collection.

So I think you should just work with the entityview, and not create a new entity collection from it, but remove it from the original collection instead. To get the original collection from the entity view, use the RelatedCollection property. Remove it from that collection, then delete it.

Frans Bouma | Lead developer LLBLGen Pro
Rono
User
Posts: 12
Joined: 04-Jan-2007
# Posted on: 09-Feb-2007 15:13:45   

Thanks Otis, that fixed it.