从实体框架的实体获取列数据类型 [英] Get Column DataType from Entity Framework Entity

查看:144
本文介绍了从实体框架的实体获取列数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先使用实体​​框架5,数据库。



是否有可能(在运行时)来获取一个实体的属性表示数据库列的数据类型?该.NET类型也将正常工作,如果这是更容易

 的IEnumerable< D​​bEntityEntry>实体= 
context.ChangeTracker.Entries()
。凡($ B $为=>
e.State == || EntityState.Added == e.State EntityState.Modified) ;

的foreach(在实体DbEntityEntry实体)
{
的foreach(在entity.CurrentValues​​.PropertyNames字符串propertyName的)
{
//所以我知道实体和属性名。我能得到的数据类型?
}
}


解决方案

使用对实体反射来获取属性信息。

 的foreach(在实体DbEntityEntry实体)
{
的foreach(在entity.CurrentValues​​.PropertyNames字符串propertyName的)
{
变种的PropertyInfo = entity.Entity.GetType()的getProperty(propertyName的)。
VAR属性类型= propertyInfo.PropertyType;

}
}


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 type would also work fine if that's easier.

IEnumerable<DbEntityEntry> entities =
    context.ChangeTracker.Entries()
            .Where(
                e =>
                e.State == EntityState.Added || e.State == EntityState.Modified);

foreach (DbEntityEntry entity in entities)
{
   foreach (string propertyName in entity.CurrentValues.PropertyNames)
   {
     //so I know the entity and the property name.  Can I get the data type?
   }
}

解决方案

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;

    }
}

这篇关于从实体框架的实体获取列数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆