How to get fields relation information?

Posts   
 
    
Posts: 15
Joined: 16-May-2012
# Posted on: 28-Jun-2012 13:08:25   

Hello,

I just trying to get field related relation information. For example i have a field from entity:

PersonFields.ClsfGenderId

How to find out if the field have relationship with other table and how to get that relation if it exists? any help will be highly appreciated. Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Jun-2012 08:31:21   

This could be a way to do that:

public List<IEntityRelation> GetRelationsFromField(IEntityCore entity, IEntityFieldCore field)
{
    var relsFound = new List<IEntityRelation>();
    var relations = entity.GetAllRelations();
    foreach (var rel in relations)
    {
        var fieldsFromPk = rel.GetAllPKEntityFieldCoreObjects();
        var fieldsFromFk = rel.GetAllFKEntityFieldCoreObjects();
        bool existOnRel = fieldsFromPk.Any(f => f.ContainingObjectName == field.ContainingObjectName && f.Name == field.Name);
        existOnRel |= fieldsFromFk.Any(f => f.ContainingObjectName == field.ContainingObjectName && f.Name == field.Name);
        if (existOnRel)
        {
            relsFound.Add(rel); 
        }
    }

    return relsFound;
}

...and the usage:

var relsFound = GetRelationsFromField(new CustomerEntity(), CustomerFields.CustomerId);
David Elizondo | LLBLGen Support Team
Posts: 15
Joined: 16-May-2012
# Posted on: 19-Jul-2012 09:13:15   

hey thanks a lot for help! do you have any idea how to get entity and pass it to the method if i have just entity name? simple_smile thanks a lot you guys doing a great job here. i just love llblgen!!

daelmo wrote:

This could be a way to do that:

public List<IEntityRelation> GetRelationsFromField(IEntityCore entity, IEntityFieldCore field)
{
    var relsFound = new List<IEntityRelation>();
    var relations = entity.GetAllRelations();
    foreach (var rel in relations)
    {
        var fieldsFromPk = rel.GetAllPKEntityFieldCoreObjects();
        var fieldsFromFk = rel.GetAllFKEntityFieldCoreObjects();
        bool existOnRel = fieldsFromPk.Any(f => f.ContainingObjectName == field.ContainingObjectName && f.Name == field.Name);
        existOnRel |= fieldsFromFk.Any(f => f.ContainingObjectName == field.ContainingObjectName && f.Name == field.Name);
        if (existOnRel)
        {
            relsFound.Add(rel); 
        }
    }

    return relsFound;
}

...and the usage:

var relsFound = GetRelationsFromField(new CustomerEntity(), CustomerFields.CustomerId);
Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 19-Jul-2012 21:35:47   
Posts: 15
Joined: 16-May-2012
# Posted on: 20-Jul-2012 08:06:02   

Yes done that already, thanks a lot.


public static object CreateObject(string typeName, object[] constructorArgs)
        {
            Type type = Type.GetType(typeName);
            object instance = Activator.CreateInstance(type, constructorArgs);

            return instance;
        }


EntityCore = (IEntityCore)Invoker.CreateObject(string.Format(ReportingNameSpaceConstants.EntityNameSpace, entityName),null);

Walaa wrote:

Check this link out: http://llblgen.com/TinyForum/Messages.aspx?ThreadID=17135