Entity Framework 4 – STE – simple DB with single table Blogs having BlogID PK column…
var samplesDbEntities = new SamplesDBEntities();
var blogId = Guid.NewGuid();
samplesDbEntities.Blogs.AddObject(new Blog() { BlogID = blogId });
var objectSetResult = samplesDbEntities.Blogs
.Where(p => p.BlogID == blogId)
.SingleOrDefault();
(result of code execution => objectSetResult == null after the last line)
AFAIK, ObjectContext is implementation of UoW pattern and in which case I guess I should get the result back from ObjectSet (Repository) just “marked as transient”
Can someone explain me what I am doing wrong and why objectSetResult has null value here?
(Yes, I am aware of ObjectStateManager, but to me it is more of a patch for the upper mentioned architectural problem)
The pattern violated in your example is not Unit Of Work pattern but Identity Mapping.
Unit of Work track changes made to objects by your code instead of you take care about that manually.
Identity Mapping pattern enacts object context to have single entity instance for single value of primary key.
It is strange for me but Entity Framework (as well as LINQ 2 SQL) does not map object identity in every situation, and situation described above is one of such cases.