Get database target field info from entity at runtime

Posts   
 
    
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 17-Feb-2012 15:20:49   

For tracing/logging purposes, I would like to extract the database target field from an entity at runtime. This would not be done in the production environment but during dev/testing. So the overhead should not be a concern.

The business case here is that we are working with legacy database names which are quite cryptic. We have aliased those name in the LLBLGen designer so that they are more recoginizable. Our business analysts would like some reassurance that the fields match the existing documentation which has the legacy database field names.

So imagine an EmployeeEntity object with some fields as shown below.

Database -> Code: EMP.EMP_L_NAME -> LastName EMP.EMP_LN -> PhoneLine EMP.EMP_PTR_ID -> PrinterId

EmployeeEntity employee = new EmployeeEntity(someValueHere);

Can I get extract the database target field from that entity? That is, given employee.LastName or EmployeeFields.LastName, I would like to trace/log an entry with three values. First, the database value of that entity field ("SMITH"), second the entityfield name (I should be able to get this with IEntityField2.Name) and the database target field name (EMP.EMP_L_NAME).

Any suggestions?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Feb-2012 19:52:23   

Hi Joel,

As you are using Adapter, you should create a method in your DataAccessAdapter class that return that. You can do that in the USER_CODE_REGION of the file or (preferred) in a partial class. Example:

namespace [RootNamespace].DatabaseSpecific
{
     public partial class DataAccessAdapter
     {
          public string GetFieldSourceName(IEntityField2 field)
          {
              return this.GetFieldPersistenceInfo(field).SourceColumnName;
          }
     }
}

So, now you can have the three values you are looking for:

var name = someField.Name;
var sourceName = new DataAccessAdapter().GetFieldSourceName(someField);
var value = someField.CurrentValue;
David Elizondo | LLBLGen Support Team