开发者

Reading newly created entity from object context

I have ObjectContext and entity, let's call it Device. Entity key of entity is not autogenerated, and is specified during adding to context. After adding of new entity with following code

Context.Devices.Add(new Device{Id = someVal, /*initialization*/});

I am trying to read that entity

开发者_高级运维var dev = Context.Devices.SingleOrDefault(d => d.Id == someVal);

and getting null.

Of course, after calling of SaveChanges() I can read that device from DB.

Why I can't read entity from context?


Because until you call SaveChanges it does not actually exist in the database. Your call to Context.Devices.SingleOrDefault(d => d.Id == someVal); is making a call back to the database.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜