Resolve propertyName from columnName

Posts   
 
    
jwijnker
User
Posts: 32
Joined: 09-Mar-2011
# Posted on: 18-Jan-2012 11:16:02   

LLBLGen v3.1 SQLSERVER 2008 R2

using some (audit) triggers in the database (for all write-actions) the tableName and columnName(s) of the involved records are logged.

Using this data I try to match an audit record back to the specific entity and entity property.

By extending the DataAccessAdapter I'm now able to get: - the tableName from an entityName - the entityName from a tableName - the columnName from an entity-field

What i need now, it to get the enity-field from a columnName (and tableName).

I found that 'PersistenceInfoProvider' holds this info, but I'm not able to get this, because its internal in the DbSpecific assembly.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jan-2012 11:33:49   

If you can get the EntityName from the TableName, and you can get the ColumnName from the EntityField of an entity. Then you can query the entity.EntityFields for the matching columnName.

jwijnker
User
Posts: 32
Joined: 09-Mar-2011
# Posted on: 18-Jan-2012 12:13:37   

Thanks for your reaction.

I'm using Adapter instead of SelfService, so I have to use the EntityFields2 class. Unfortunatly, EntityFields2 doesn't contain the columnName and also no propertyName.

Is there also a solution using Adapter?

EDIT:

I Get the fieldInfos using DataAccessAdapter.GetFieldPersistenceInfos(entityName.ToString()).

Here I have a IFieldPersistenceInfo[] for the given entity, which contains the ColumnName, but no propertyName.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jan-2012 16:58:54   

Use GetFieldPersistenceInfo() without the 's' it accepts IEntityField2

protected virtual IFieldPersistenceInfo GetFieldPersistenceInfo(IEntityField2 field)

Used as follows.

IFieldPersistenceInfo fieldInfo = adapter.GetFieldPersistenceInfo(myEntityField);
var columnName = fieldInfo.SourceColumnName;

So you can loop on the entity.Fields, and for each Entity field, you'd have the entityField.Name property and the columnName as shown from the above code.

jwijnker
User
Posts: 32
Joined: 09-Mar-2011
# Posted on: 19-Jan-2012 01:17:36   

Walaa wrote:

Use GetFieldPersistenceInfo() without the 's' it accepts IEntityField2

protected virtual IFieldPersistenceInfo GetFieldPersistenceInfo(IEntityField2 field)

Used as follows.

IFieldPersistenceInfo fieldInfo = adapter.GetFieldPersistenceInfo(myEntityField);
var columnName = fieldInfo.SourceColumnName;

So you can loop on the entity.Fields, and for each Entity field, you'd have the entityField.Name property and the columnName as shown from the above code.

Walaa, thanks again for your reply.

As in my first post, I already found that the columnName is a property of IFieldPersistenceInfo. (I get the tableName and a columnName from somewhere else...) What I try to do, is to get the associated propertyName for it.

i.e.: DBTableName: Person DBColumnName: personId // These two is what i have...

mapped with Designer to:

EntityName: UserEntity // This name is what I can resolve... **EntityField: Id // This is the one I'm looking for. ** (propertyName)

Using the GetFieldPersistenceInfoS I can get the all fields for an enity. Filtering the resulting FieldPersistenceInfo[] on the SourceColumnName, I found the corresponding fieldInfo. But unfortunatly, FieldPersistenceInfo doesn't contain the propertyName for the entity. That's what I'm looking for.

I also found that IFieldInfo does contain the propertyName, but I didn't find a way (possibly because I use Adapter instead of SelfService) to get the IFieldInfo from an entity, IFieldPersistenceInfo or 'someFactory'.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Jan-2012 10:03:37   

Here is a complete tested code.

1- Derive from DataAccessAdapter to add the following methods


public partial class DataAccessAdapterEx: DataAccessAdapter
{
    public new IFieldPersistenceInfo GetFieldPersistenceInfo(IEntityField2 field)
    {
        return base.GetFieldPersistenceInfo(field);  // exposes the protected method
    }

    public string GetTableName(string entityName)
    {
        return this.GetFieldPersistenceInfos(entityName)[0].SourceObjectName;
    }
}

2-

public IEntity2 GetEntityFromTableName(string tableNameToFind)
{
    IEntity2 toReturn;
    using (var adapter = new DataAccessAdapterEx())
    {
        var entityAsString = (from e in Enum.GetNames(typeof(EntityType))
                              let tableName = adapter.GetTableName(e)
                              where tableName == tableNameToFind
                              select e).FirstOrDefault();

        toReturn = Northwind.FactoryClasses.GeneralEntityFactory.Create(
            (EntityType)Enum.Parse(typeof(EntityType), entityAsString));
    }

    return toReturn;
}

public string GetEntityFieldName(IEntity2 entity, string columnName)
{
    var fieldName = string.Empty;

    using (var adapter = new DataAccessAdapterEx())
    {
        foreach (IEntityField2 field in entity.Fields)
        {
            IFieldPersistenceInfo fieldInfo = adapter.GetFieldPersistenceInfo(field);
            if (fieldInfo.SourceColumnName == columnName)
                return field.Name;
        }
    }

    return fieldName;
}

3- Test the code as follows:

IEntity2 entity = GetEntityFromTableName("Customers");
var propertyName = GetEntityFieldName(entity, "CustomerID");