Answered by:
ChangeTracker is true, Which field changed?

Question
-
I use DBContext ChangeTracker HasChanges method to determine if my record needs to be saved. My record has 270 fields.
The HasChanges method mysteriously is true. I don't know which field has changed. Is there a way to determine which field in the record has changed?
Certified Geek
Tuesday, June 24, 2014 2:18 PM
Answers
-
Hello,
>>I have a problem with DbContext.Entry. What does Entry represent?
What is your problem? Do you meet any issue? The DbContext.Entry is an object for the given entity providing access to information about the entity and the ability to perform actions on the entity. The DFDBEntities is the context class generated by the model automatically.
Regards.
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Monday, June 30, 2014 6:17 AM
All replies
-
Hello,
>>Is there a way to determine which field in the record has changed?
Yes, you can write code as below to check whether which field is changed:
Bool isChanged = DbContext.Entry<EntityType>(entityInstance).Property("PropertyName").IsModified;
Here, I made an example:
using (DFDBEntities db = new DFDBEntities()) { var result = db.Orders.FirstOrDefault(); result.OrderName = "Changed"; Type entityType = result.GetType(); List<PropertyInfo> entityProperties = entityType.GetProperties().ToList(); foreach (PropertyInfo entityProperty in entityProperties) { if (!((entityProperty.PropertyType).Namespace == "System.Collections.Generic")) { Console.WriteLine("Field " + entityProperty.Name + " is modified:" + db.Entry<Order>(result).Property(entityProperty.Name).IsModified); } } //db.SaveChanges(); }
Using the if (!((entityProperty.PropertyType).Namespace == "System.Collections.Generic")) condition is to except the navigate properties.
The result:
If this does not work for you, please let me know.
Regards.
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Wednesday, June 25, 2014 2:14 AM -
I am trying to apply your code to my situation.
I have a problem with DbContext.Entry. What does Entry represent? Please publish your using statements.
Certified Geek
Friday, June 27, 2014 3:32 PM -
Hello,
>>I have a problem with DbContext.Entry. What does Entry represent?
What is your problem? Do you meet any issue? The DbContext.Entry is an object for the given entity providing access to information about the entity and the ability to perform actions on the entity. The DFDBEntities is the context class generated by the model automatically.
Regards.
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Monday, June 30, 2014 6:17 AM -
Code does not compile without proper using statements. Please publish the using statements for your code.
Certified Geek
- Edited by Arne MN Tuesday, July 1, 2014 11:32 AM
Tuesday, July 1, 2014 11:31 AM -
Most likely what you are missing is the using statement for Reflection classes. Try the following using statement.
using System.Reflection;
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Tuesday, July 1, 2014 2:25 PM -
I found my typo. The original answer was correct.
Certified Geek
Thursday, July 3, 2014 12:57 PM