Get Column DataType from Entity Framework Entity

ぐ巨炮叔叔 提交于 2019-12-01 09:06:01

Use reflection on the entity to get the property info.

foreach (DbEntityEntry entity in entities)
{
    foreach (string propertyName in entity.CurrentValues.PropertyNames)
    {
        var propertyInfo = entity.Entity.GetType().GetProperty(propertyName);
        var propertyType = propertyInfo.PropertyType;

    }
}

To get the data type of the particular column of the table:

[Assume: Entity prop class name: Vendors and column name ="VendorID"]

string columnTypName =   (context.Vendors.EntitySet.ElementType.Members["VendorID"].TypeUsage.EdmType).Name;

To get all columns names and type of the columns dynamically:

[Param : tableName]

var columns = 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 == tableName)
                       select new
                       {
                           colName = p.Name,
                           colType = p.TypeUsage.EdmType.Name
                       };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!