开发者

Entity Framework 4: Repository method T Add(T entity) returns T: Can that actually happen (without SaveChanges)?

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).Singl开发者_开发问答e();

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.


_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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜