Entity Framework pull object from memory
I've been use entity framework, always thinks that entity framework created an abstracted layer to programmer, but i've been asking if this kind code its possible. When I create an entity and dont save db context, this object cant be pulled from the context!? Wired or I am be confused about EF!?!
using (Entites db = new Entites())
{
tblSysState state = new tblSysState()
{
Id = Guid.NewGuid(),
Code = "k",
Description ="Just teste"
开发者_开发技巧 };
db.tblSysState.AddObject(state);
Object ft = db.tblSysState.SingleOrDefault(x => x.Code.Equals("k"));
}
It is possible but not with querying ObjectSet
itself. You must access context's internal storage. Something like this should work:
var state = db.ObjectContext
.GetObjectStateEntries(EntityState.Added)
.Where(e => !e.IsRelationship)
.Select(e => e.Entity)
.OfType<tblSysState>()
.SingleOrDefault(e => e.Code == "K");
精彩评论