The line of code does not work if not executed using a breakpoint

The only thing I can think of is the race condition, but both the calling function and the line of code are synchronous with my knowledge.

/// <summary> /// Gets the log format string for an info-level log. /// </summary> public static string Info<T>(string action, T obj) { var stringBuilder = new StringBuilder(String.Format( "Action: {0} \tObject: {1} \tUser: {2} \tJson: ", action, typeof(T).Name, User )); // Set all virtual properties to null. This should stop circular references of navigation properties. var virtualProperties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(x => x.GetSetMethod().IsVirtual && !x.PropertyType.IsPrimitive); foreach (var propInfo in virtualProperties) { propInfo.SetValue(obj, null); // This Line is the culprit. } GetJsonSerializer().Serialize(obj, stringBuilder); return stringBuilder.ToString(); } 

The line propInfo.SetValue(obj, null) will be executed if I just stop before the loop and step by step (or just go to this line), however, if I do not use a breakpoint, it never sets property (s) to null . Why is this?

Specific Information :

  • If I do not use a breakpoint, it does not work.
  • If I put a breakpoint at the top of the foreach and press f5, it does not work.
  • If I put a breakpoint at the top of the foreach and step by step through f10, it works.
  • If I put a breakpoint on a line of code propInfo.SetValue(obj, null); , she works.
  • The breakpoint after the loop still shows the values ​​as non-zero.
  • If I change null to 5 (this is an invalid value), it throws an exception telling me that this is an invalid value.

To clarify, β€œDoesn't work” means that it does not set the null property.

What I tried:

  • Restarting Visual Studio (2013)
  • Change a line of code (used for default(T) )
  • Project Properties β†’ Assembly β†’ Optimize Code (initially disabled)

EDIT

It has narrowed that the reason for this behavior is the properties of EF Navigation. The code works, but for some reason, the navigation properties refuse to become null. So, what about navigational properties causing this behavior?

+7
c # entity-framework entity-framework-6
source share
2 answers

Lazy loading

The navigation properties were lazy, so when the serializer looked at them, they were overwritten with the original value. Thus, the null setting worked all the time, but was overwritten by lazy loading.

Debugging

The reason that debugging appeared the way it was was because I looked at the value before I executed the line of SetValue code. This caused the navigation property to load the value before executing the line of code, as a result of which the null value was not overwritten.

Decision

 foreach (var propInfo in virtualProperties) { propInfo.GetValue(obj); // Trigger any navigation property to load. propInfo.SetValue(obj, null); } 
+5
source share

I had a VERY similar problem in the first setup of EF6 code for many to many. In my Web API controller, I set the object navigation property from the values ​​set from the DTO. One of them was transferred to my repo, the properties were not updated due to lazy loading, but in debug mode it would work if I approached this part of the code. I ended up using:

 try { TheDatabase.Configuration.LazyLoadingEnabled = false; ... ... } finally { TheDatabase.Configuration.LazyLoadingEnabled = true; } 

This seems to work so far. Since my dbContext is hosted on the controller, I don't think this will be a problem.

0
source share

All Articles