Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to call</p> <pre><code>samplesDbEntities.SaveChanges(); </code></pre> <p>before requerying for the object.</p> <pre><code>var samplesDbEntities = new SamplesDBEntities(); var blogId = Guid.NewGuid(); samplesDbEntities.Blogs.AddObject(new Blog() { BlogID = blogId }); samplesDbEntities.SaveChanges(); var objectSetResult = samplesDbEntities.Blogs .Where(p =&gt; p.BlogID == blogId) .SingleOrDefault(); </code></pre> <p><strong>Update</strong></p> <p>The reason why you are not getting the added user back in objectSetResult is that calling the SingleOrDefault method of an IQueryable object results in a database query (an actual SQL query string is generated accoding to the "where" condition, etc.), and since the object is not (yet) in the database, it doesn't get returned. The new object is, however, attached to the context, and it's EntityState is set to "Added". According to MSDN, objects in the Added state do not have original values in the ObjectStateEntry. The state of objects inside an object context is managed by the ObjectStateManager. So if you want to check that the object has really been attached, you can fetch it by calling GetObjectStateEntry:</p> <pre><code>var samplesDbEntities = new SamplesDBEntities(); Blog blog = new Blog() { BlogID = Guid.NewGuid() }; samplesDbEntities.Blogs.AddObject("Blogs", blog); Blog addedBlog = (Blog)context.ObjectStateManager.GetObjectStateEntry(blog).Entity; </code></pre> <p>Also note that the EntityState of the retrieved object is "Added".</p> <p>To sum up - regarding your initial question about whether it is a correct implementation of UnitOfWork, I don't see why not. It does indeed maintain a list of objects and tracks the changes, etc, etc. The issue you encountered, however, is related to the fact that the you are fetching data from the underlying provider, rather from the list of objects currently attached to the context.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload