[英]Entity Framework get properties including some values
I have a method which iterates through all the properties of an object. 我有一个遍历对象所有属性的方法。 I am logging those properties:
我正在记录这些属性:
Object obj = entry.Entity;
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
oldData.AppendFormat("{0}={1} || ", property.Name, property.GetValue(obj, null));
}
Now this is working fine but on my table log, it is also writing this properties below: 现在这可以正常工作,但是在我的表日志中,它也在下面编写了此属性:
- PremiumReference=System.Data.Objects.DataClasses.EntityReference`1[Data.Premium]
- EntityState=Deleted
- EntityKey=System.Data.EntityKey
Any ideas how I can filter this properties? 有什么想法可以过滤此属性吗?
Every Entity in Entity Framework has a property with the enumeration EntityState
. 实体框架中的每个实体都有一个带有
EntityState
枚举的EntityState
。 EF adds them to the object. EF将它们添加到对象中。
If you add an Object to EF it marks it as EntityState.Added. 如果将对象添加到EF,则将其标记为EntityState.Added。
Hope it helps. 希望能帮助到你。
Have a look in here 在这里看看
BindingFlags-Enumeration 的BindingFlags的枚举
Maybe it helps using the flag DeclaredOnly in combination with the other flags you need in your scenario to match your needs. 可能有助于将DeclaredOnly标志与方案中需要的其他标志结合使用,以匹配您的需求。
I solved my issue with this code below: 我用下面的代码解决了我的问题:
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
.Where(pi => !(pi.PropertyType.IsSubclassOf(typeof(EntityObject))) && !(pi.PropertyType.IsSubclassOf(typeof(EntityReference))))
.ToArray();
The BindingFlags
did help but I do not also want the EntityReference
and EntityObject
so I needed to add the where
clause. BindingFlags
确实有帮助,但是我也不想EntityReference
和EntityObject
所以我需要添加where
子句。
How to get all names of properties in an Entity? 如何获取实体中所有属性的名称?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.