I have a Code First app with Entity Framework 6. I've run across a problem where EF seems to be messing with my constructors in bizarre ways.
public class ParentClass
{
public int ParentClassId { get; set; }
public string SomeField { get; set; }
public IList<ChildClass> ChildClasses { get; set; }
public ParentClass()
{
BodySingleton.Instance.Strings.Add(SomeField);
}
public class ChildClass
{
public int ChildClassId { get; set; }
public string SomeOtherField { get; set; }
public ChildClass()
{
BodySingleton.Instance.Strings.Add(SomeOtherField);
}
}
}
public sealed class BodySingleton
{
public List<string> Strings { get; set; }
static BodySingleton()
{
}
private BodySingleton()
{
Strings = new List<string>();
}
public static BodySingleton Instance { get; } = new BodySingleton();
}
The above is an approximation of the live code, which is much more complex. I haven't set up EF for this psuedo code so I haven't actually run the above, but I believe it should have the same problem.
So what happens here is the constructor for ParentClass
works as intended, but ChildClass
does not. What's weird is that some things with ChildClass
work; for instance, if I add a line like SomeOtherField = "Hello, world";
to the constructor, that will work as intended. But it seems like anything I do that tries to interact with other objects do not work. I've also found that debug breaks work with the ParentClass
but are completely bypassed by the ChildClass
constructor.
Any idea what's happening here? All I can figure is that it's some bug with EF, but it's driving me bonkers and I can't figure out a workaround.