Get Column DataType from Entity Framework Entity

后端 未结 2 993
感情败类
感情败类 2021-01-14 17:38

Using Entity Framework 5, database first.

Is it possible (at run time) to get the data type of the database column that an entity\'s property represents? The .net t

2条回答
  •  再見小時候
    2021-01-14 17:54

    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;
    
        }
    }
    

提交回复
热议问题