WayneBrantley wrote:
Well, I understand, but shouldn't 'alreadybeenfetched' set to true once the entity has been requested and a 'new' one returned?
No, it's not, as that would possibly increase the graph without you noticing it. It's an old debate: what should myOrder.Customer return if the Customer doesn't exist? Null or a new entity? So you have a choice: by default it's a new entity (but not wired to the graph), though you can have it return null, by setting the preference / project property: LazyLoadingWithoutResultsReturnsNew to false. It's true by default. (for backwards compatibility).
Where I ran across this is I had a function like:
void LoadValues(MyEntityN entityN)
{
MyEntity1 entity1 = entityN.MyEntity1;
entity1.SomeProperty=1;
entity2.SomeProperty1="Test";
.....
}
I would call this code with a MyEntityN object that may or may not have an associated MyEntity1 object.
What happens is, this code works if the MyEntity1 object exists or has been assigned already. If it does not, then the code does not work. The workaround is to write all code modifying subentities to be this:
void LoadValues(MyEntityN entityN)
{
MyEntity1 entity1 = entityN.MyEntity1;
entity1.SomeProperty=1;
entity2.SomeProperty1="Test";
.....
entityN.MyEntity1=entity1;
}
If you say that is the way it is and has to be, fine - I will make sure all my code is written with this concept in mind - however, if this can be changed - I think it would make sense to return the same object each time it is requested - regardless if the object was 'new' and created on the fly or already existed.
You either receive a new entity that's not yet associated with the entity you called the property on, or you receive null if you've set the project property (and regenned the code).
The reason it returns a new entity and not null by default is to avoid nullreference exceptions in code which assumes there's always an entity. This is by design since the beginning, though you have the choice to let it return null through the project property.