开发者

Reading newly created entity from object context

开发者 https://www.devze.com 2023-04-09 23:58 出处:网络
I have ObjectContext and entity, let\'s call it Device. Entity key of entity is not autogenerated, and is specified during adding to 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

精彩评论

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