What this post is all about is the following program written in C# using Entity Framework 6.1.3 throwing at line 25 and not at line 23.
We can see the simplest usage of Entity framework here. There is a
Test class and a
TestChild class which contains a reference to an instance of
Parent. This reference is marked as
virtual so that Entity Framework in instructed to load an instance in a lazy manner, i.e. upon first usage of that reference. In DDL model obviously
TestId column is a foreign key to
I create an entity object, save it into database and then I retrieve it at line 21. Because the class uses virtual properties, Entity Framework dynamically creates some custom type in order to be able to implement lazy behavior underneath.
Now let’s suppose I have a need to modify something in an object retrieved from a database. It turns out I can modify
Value property, which is of pure
string type. What is more, I can also modify
Parent property, but… the modification is not preserved!. This program throws at line 25 because an assignment from line 24 is silently ignored by the framework.
I actually have been trapped by this when I was in need of modifying some collection in complicated object graph. I am deeply disappointed the Entity Framework on one hand allows modification of non-virtual properties, but on the other hand it ignores virtual ones. This can make a developer run into a trouble.
Of course I am aware it is not good practice to work on objects of data model classes. I recovered myself from this situation with AutoMapper. But this is kind of a quirk, and a skilled developer has to hesitate to even try to modify something returned by Entity Framework.
Leave a Reply