Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 775
Gerald Gonzales Avatar asked Sep 13 '26 07:09

Gerald Gonzales


2 Answers

Every Entity in Entity Framework has a property with the enumeration EntityState. EF adds them to the object.

If you add an Object to EF it marks it as EntityState.Added.

Hope it helps.

See Entity Framework EntityState

like image 136
Lukas Abfalterer Avatar answered Sep 15 '26 21:09

Lukas Abfalterer


Have a look in here

BindingFlags-Enumeration

Maybe it helps using the flag DeclaredOnly in combination with the other flags you need in your scenario to match your needs.

like image 26
Dom84 Avatar answered Sep 15 '26 20:09

Dom84