This is a migrated thread and some comments may be shown as answers.

IPersistentTypeDescriptor

2 Answers 25 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
devoas
Top achievements
Rank 1
devoas asked on 15 Dec 2009, 10:22 PM

Hello,

I am trying to get the Primary / Identify Field PropertyName to be used in dynamic Query. I tried  following code

 

 

IPersistentTypeDescriptor ptd = scope.PersistentMetaData.GetPersistentTypeDescriptor(typeof(T));

 

 

PropertyDescriptorCollection pdc = ptd.GetProperties();

 

 

foreach (var item in ptd.IdentityFields)

 

{

sPrimaryKey = item.Name.ToString();

 

break;

 

}



But this is giving the field name instead of Actual properly name holding the primary key.

Please advise how to get that.

Thanks

2 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 16 Dec 2009, 05:36 PM
Hello devoas devoas,

Retrieving the property name via the OpenAccess' API is not possible at the moment. However, when reverse mapping is used the property names are generated from the field name by capitalizing the first letter. You can use this approach (capitalize the first letter) if you have not changed the property names manually.

Otherwise you could use the FieldAliasAttribute in order to find the property name for particular field. The FieldAliasAttribute marks the property as an alias for a given field. Before you try the provided solution please make sure there are generated FieldAliasAttributes at least for the identity fields inside the persistent classes.

public static string GetPropertyName(Type t, string fieldName)
{
    foreach (PropertyInfo pi in t.GetProperties())
    {
        foreach (FieldAliasAttribute attribute in pi.GetCustomAttributes(typeof(FieldAliasAttribute), true))
        {
            if (attribute.FieldName == fieldName)
            {
                return pi.Name;
            }
        }
    }
    return null;
}
 
...
IPersistentTypeDescriptor ptd =
    scope.PersistentMetaData.GetPersistentTypeDescriptor(typeof(Order));
 
PropertyDescriptorCollection pdc = ptd.GetProperties();
 
foreach (var item in ptd.IdentityFields)
{
    string propertyName = GetPropertyName(typeof(Order), item.Name);
    Console.WriteLine(propertyName);
}
...

Hope that helps.

Greetings,
Damyan Bogoev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
devoas
Top achievements
Rank 1
answered on 22 Dec 2009, 08:37 PM
Its working and this is exactly what I was looking for.

Thanks alot.

Tags
General Discussions
Asked by
devoas
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
devoas
Top achievements
Rank 1
Share this question
or