Entity Framework - how do I get the columns?

后端 未结 7 1535
后悔当初
后悔当初 2020-12-01 14:25

I wish to get a list of columns names, types and whether the column is a PK of a table object in Entity Framework.

How do I do this in C# (4.0) (ideally generically)

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 14:50

    If you do not want to use reflection, see answer here. Replace entity name below with your entity name

    var cols = from meta in ctx.MetadataWorkspace.GetItems(DataSpace.CSpace)
                           .Where(m=> m.BuiltInTypeKind==BuiltInTypeKind.EntityType)
                        from p in (meta as EntityType).Properties
                           .Where(p => p.DeclaringType.Name == "EntityName")
                       select new
                          {
                           PropertyName = p.Name,
                           TypeUsageName = p.TypeUsage.EdmType.Name, //type name
                           Documentation = p.Documentation != null ?               
                                           p.Documentation.LongDescription : null //if primary key
            };
    

提交回复
热议问题