c# - Caching Entities causes unwanted inserts -
if cache entire table:
static list<table1> table1cache = context.table1.tolist();
then use associate:
var context = new context(); var t2 = new table2(); t2.mytable1reference = table1cache.single(x=>x.id == paramintid); context.savechanges();
a new row inserted table1, because of third line. ef thinks new entity. know can somethings attaching cache when create de context(i have 1 context per request), or use mytable1referenceid = table1cache.single(x=>x.id == paramintid).id;
but not secure, can forget sometimes, there solution?
yes, makes sense because entity not associated current context. therefore ef thinks it's transient , saves new instance.
if caching across contexts, don't want store object itself. related context. instead want store data in cache. serializing , deserializing entity. need associate entity when current context next time it's retrieved cache can save change both cache , database.
if sounds lot, is. keeping 2 data stores synchronized not easy problem solve. take @ implementation of 2nd level cache nhibernate.
Comments
Post a Comment