gpt4 book ai didi

c# - 从 Entity Framework 实体获取列数据类型

转载 作者:行者123 更新时间:2023-11-30 15:32:31 25 4
gpt4 key购买 nike

首先使用 Entity Framework 5,数据库。

是否有可能(在运行时)获取实体属性所代表的数据库列的数据类型?如果更容易的话,.net 类型也可以正常工作。

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

最佳答案

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

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

}
}

关于c# - 从 Entity Framework 实体获取列数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18901720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com