Deleting Item Only if Item has no relationships

Posts   
 
    
hameedAEM1
User
Posts: 17
Joined: 14-Sep-2006
# Posted on: 04-Oct-2006 12:32:16   

Hi All,

I am using Adapter/VS2005/C#

I have three tables A, B and C.

A is related to B (1:m) and C is related to B (1:m).

A -< B >- C

Now if i wanted to delete an row in A and hence delete all relationships in table B. But only delete the related row in C if only it does not have a FK relationship with another row.

I thought of using GetDependingRelatedEntities/GetDependentRelatedEntities methods but neither of these return anything (I am using Prefetch to get all data first).


           BrochureItemEntity b = (BrochureItemEntity)dataGridView1.CurrentRow.DataBoundItem;

            if (b != null)
            {
                using (DataAccessAdapter da = new DataAccessAdapter())
                {
                    IPrefetchPath2 ppath = new PrefetchPath2((int)EntityType.BrochureItemEntity);
                    IPrefetchPathElement2 path = ppath.Add(BrochureItemEntity.PrefetchPathBrochureItemContentText);
                    path.SubPath.Add(BrochureItemContentTextEntity.PrefetchPathContentText);

                    da.KeepConnectionOpen = true;
                    da.FetchEntity(b, ppath);

                    UnitOfWork2 work = new UnitOfWork2();

                    if (b.BrochureItemContentText != null)
                    {
                        if (b.BrochureItemContentText != null)
                        {
                            foreach(BrochureItemContentTextEntity c in b.BrochureItemContentText)
                            {
                                if (c.ContentText != null)
                                {
                                    [colorvalue="FF0000"]// -- this is where I need to determine if the item has other relationships. if not then add to uow for deletion.[/color]
                                    
                                }
                            }                           
                            
                            work.AddCollectionForDelete(b.BrochureItemContentText);                         
                        }                       
                    }

                    work.AddForDelete(b);

                    work.Commit(da, true);
                }
            
            }

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Oct-2006 15:57:09   

You did not add the prefetchPathElement path to the PrefetchPath ppath.

hameedAEM1
User
Posts: 17
Joined: 14-Sep-2006
# Posted on: 04-Oct-2006 16:49:34   

Walaa,

I'm not sure what you mean since the prefetchlelement is added to the parent prefetchpath. This works fine and I can see all the data returned in the query.

THe problem I am having is getting the dependent entities so I can determine if this row has a foreignkey relationship with other rows or is this it,in the case I can delete the row.

hameed.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Oct-2006 16:52:32   

Sorry my mistake, I didn't notice it.

Would you please post code snippet containing the usage of GetDependingRelatedEntities & GetDependentRelatedEntities, coz I think these should work.