IPersistentTypeDescriptor

Thread is closed for posting
3 posts, 0 answers
  1. devoas
    devoas avatar
    71 posts
    Member since:
    Dec 2009

    Posted 15 Dec 2009 Link to this post

    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. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 16 Dec 2009 Link to this post

    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.
  3. devoas
    devoas avatar
    71 posts
    Member since:
    Dec 2009

    Posted 22 Dec 2009 Link to this post

    Its working and this is exactly what I was looking for.

    Thanks alot.

Back to Top