Using GetAllRelations() while deleting entry in a table/entity

Posts   
 
    
Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 18-Jan-2012 14:17:05   

I want to delete a entity. But, it has many one to many relations set. So, I want to make sure that there is no dependent data in db.

I looked into the post http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=17715&HighLight=1. But, it didn't help me with latest version of LLBLGen 3.1 (licensed version). Using the above post, I have created my code snippet as follows. Please help me to load related objects using the parent object relation.

My Code snippet is given below:

  var list = ((IEntity2) actual[0]).GetAllRelations();
            string str = string.Empty;
               using (var adapter = new DataAccessAdapter())
               {
                   foreach (IEntityRelation entityRelation in list)
                   {
                      IEntityFieldCore fc  = entityRelation.GetFKEntityFieldCore(0);
                       string en = fc.ContainingObjectName;

                       EntityType typeOfEntity = (EntityType) Enum.Parse(typeof(EntityType), en);

                       IEntity2 entityInstance = FactoryClasses.GeneralEntityFactory.Create(typeOfEntity);
                       IEntityCollection2 ec = entityInstance.GetEntityFactory().CreateEntityCollection();
                    }
            }
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jan-2012 16:02:06   

I want to make sure that there is no dependent data in db.

If there are dependant data, would you cancel the delete action, or would you delete the dependants first?

Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 18-Jan-2012 18:02:24   

I will be prompting user, if I find dependent data. Based on his response, I have three scenario here.

  1. to delete dependent data
  2. to nullify fields in dependent tables.
  3. cancel the whole delete initiative
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Jan-2012 10:40:02   

The above posted code snippet seems ok, you did not specify what's the problem with it?

Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 19-Jan-2012 11:18:44   

I have copied complete method here. In last 10 lines, I am trying to add filter to the query and also find count. I am struggling in this part. Your help will be appreciated.

            var target = new Personnel();
            const long incidentId = 1;
            IList actual = target.Find(incidentId);
            var list = ((IEntity2)actual[0]).GetAllRelations();

            string str = string.Empty;
            using (var adapter = new DataAccessAdapter())
            {

                var metaData = new LinqMetaData(adapter);
                foreach (IEntityRelation entityRelation in list)
                {
                    IEntityFieldCore fc = entityRelation.GetFKEntityFieldCore(0);
                    IEntityFieldCore fc1 = entityRelation.GetFKEntityFieldCore(1);
                    string en = fc.ContainingObjectName;
                    string en1 = fc1.ContainingObjectName;

                    EntityType typeOfEntity = (EntityType)Enum.Parse(typeof(EntityType), en);
                    EntityType typeOfEntity1 = (EntityType)Enum.Parse(typeof(EntityType), en1);

                    IEntity2 entityInstance = AmbuPro.DAL.FactoryClasses.GeneralEntityFactory.Create(typeOfEntity);
                    IEntityCollection2 ec = entityInstance.GetEntityFactory().CreateEntityCollection();
                    
                    IPredicateExpression bucket = new PredicateExpression();
                    bucket.Add(new FieldCompareValuePredicate((IEntityField2)fc, ComparisonOperator.Equal, 1));
                    bucket.Add(new FieldCompareValuePredicate((IEntityField2)fc1, ComparisonOperator.Equal, 1));
                    IRelationCollection rs = new RelationCollection();
                    rs.Add(entityRelation);
                  long c = Convert.ToInt64(ec.Count);
if (entityRelation.TypeOfRelation == RelationType.OneToMany &&
                        entityRelation.TypeOfRelation == RelationType.OneToOne)
                        str += " " + entityRelation.MappedFieldName;
                }
            }
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Jan-2012 12:34:55   

I'm not sure what your code is doing and what exactly that you are struggling with in the last 10 lines. You should be more specific in your questions.

Anyway, here is what I think you need, tested in Northwind:

var customer = new CustomerEntity("ALFKI");
var relations = ((IEntity2)customer).GetAllRelations();

using (var adapter = new DataAccessAdapter())
{
    foreach (IEntityRelation relation in relations)
    {
        if (relation.TypeOfRelation != RelationType.OneToMany)
            continue;

        var field = relation.GetFKEntityFieldCore(0);
        EntityType typeOfEntity = (EntityType)Enum.Parse(typeof(EntityType), field.ContainingObjectName);
        IEntity2 entityInstance = GeneralEntityFactory.Create(typeOfEntity);
        IEntityCollection2 relatedCollection = entityInstance.GetEntityFactory().CreateEntityCollection();
        if (adapter.GetDbCount(relatedCollection, new RelationPredicateBucket((EntityField2)field == customer.CustomerId)) > 0)
        { 
            //prompt user
            return;
        }
    }
}

Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 19-Jan-2012 16:21:37   

That helped me.. thanks..