Print Page | Close Window

Getting primary key information for arbitrary Entity type

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=853
Printed Date: 11-Jun-2026 at 1:37am


Topic: Getting primary key information for arbitrary Entity type
Posted By: vecs
Subject: Getting primary key information for arbitrary Entity type
Date Posted: 25-Jun-2008 at 6:21am
How to get meta-information about the primary key fields for arbitrary Entity type?
May be in form of PropertyDescriptor collection or something else?



-------------



Replies:
Posted By: GeoffAtDatagaard
Date Posted: 25-Jun-2008 at 7:45am
Im a newbie too, so I'd appreciate knowing an easier way than this
(Maybe a primary key collection somewhere)
 
foreach (EntityColumn ec in EntityColumn.GetEntityColumns(typeof(Employee))) {
if (ec.IsPrimaryKeyColumn) {
  // This is a primary key column
}
}
 
or
 
For Each ec As EntityColumn In EntityColumn.GetEntityColumns(GetType(Employee))
 If ec.IsPrimaryKeyColumn Then
  ' This is a primary key column
 End If
Next


Posted By: davidklitzke
Date Posted: 25-Jun-2008 at 11:06am
IdeaBlade does not have such a thing as a Primary Key collection, so this is a good solution.


Posted By: f3rland
Date Posted: 25-Jun-2008 at 11:33am
Generic function to retreive PKColumn list in VB.NET
Adjust it to fit your needs

   Private Function GetPK(Of t As IdeaBlade.Persistence.Entity)() As Collections.Generic.List(Of IdeaBlade.Persistence.EntityColumn)
      Dim oResult As New Collections.Generic.List(Of IdeaBlade.Persistence.EntityColumn)

      For Each ec As IdeaBlade.Persistence.EntityColumn In IdeaBlade.Persistence.EntityColumn.GetEntityColumns(GetType(t))
         If ec.IsPrimaryKeyColumn Then
            oResult.Add(ec)
         End If
      Next

      Return oResult
   End Function

Usage
Dim oList As Collections.Generic.List(Of IdeaBlade.Persistence.EntityColumn)
oList = GetPK(Of Model.Employee)



-------------
F3


Posted By: vecs
Date Posted: 26-Jun-2008 at 12:47am
Thanks to all!

Here is the C# code I finally use:

    public static class IbUtils
    {
        public static List<EntityColumn> GetPKColumns(Type pEntityType)
        {
            List<EntityColumn> pkColumns = new List<EntityColumn>();
            foreach (EntityColumn column in EntityColumn.GetEntityColumns(pEntityType))
            {
                if (column.IsPrimaryKeyColumn)
                    pkColumns.Add(column);
            }
            return pkColumns;
        }
        //...
    }

Usage example:

        List<EntityColumn> pkColumns = IbUtils.GetPKColumns(someEntityType);





Print Page | Close Window