views:

20

answers:

1

If you have an interface for a repository that includes

T Add( T entity);

The Repository wouldn't include a Save() or SaveChanges(). If you were to return "entity" with:

return _dc.Entities.Where( n => n.ID == entity.ID).Single();

I wouldn't expect this to hit the database and auto-generate identity values (auto-incrementing). Two questions:

  1. What values would be in the fields for ID assuming it is an identity (auto-incrementing) field?
  2. When this actually does save, can we expect the identity fields to automatically update in the returned object?
  3. Is there a command that will update object references from the database outside a repository?

This is using ASP.NET POCO Generator for Entity Framework 4 with virtual properties.

+1  A: 

_dc.Entities.Anything will always hit the DB. LINQ to Entities always hits the db. Even if the object is already in the context.

To avoid a DB query if the object is already in the context, use ObjectContext.GetObjectByKey().

An auto-incremented int or long will be 0 before you save. Yes, this gets updated when you save. As long as the objects are attached to the context, they'll all see this.

Craig Stuntz
That's the ticket. Thanks!
Dr. Zim