Hey guys alright, here is the script:
VS 2008.NET 3.5 SP1, LINQ to SQL.
I have a Product Entity that has an ImageID field, so it is related to Image Entity. All of this is defined in schema and DBML.
Now, let's say I create a new Product object without an ImageID and save it to the database. At this point, the _Image field of the object has a HasLoadedOrAssignedValue of false, as it should be.
Now, when I go and retrieve this object from the database, without an image identifier, that is, ImageID is set to null, and the image property associated with it is also null, HasLoadedOrAssignedValue is true ... why ??
Now, when I try to set the ImageID property to a valid int, I get a ForeignKeyReferenceAlreadyHasValueException.
Now I know that people say: βOh, you must set the relationship property yourself, so Product.Image = someImage, not Product.ImageID = 2,β but I always set the ID field and it always works .... well , almost always
So my question is twofold:
- Why is HasLoadedOrAssignedValue set to true if it should not
- More importantly, why am I experiencing such inconsistent behavior. I know that you donβt always need to set the entity property directly, so why doesn't this work this time? And if it's a LINQ to SQL rule, then why can a rule be broken sometimes?
Hooray!
source share