R
R
Ruslan2015-03-24 21:45:43
C++ / C#
Ruslan, 2015-03-24 21:45:43

How to cache Entity Framework results without Lazy Load and connected DB connection?

Hello.
I want to organize caching of entities extracted from the repository through EF6 in a static property.
I have this code:
SPUserInfo user = _r.SPUserInfo.SingleOrDefault(x => x.UserId == UserId);
After I have cached the user object, I can access other related entities of this object through Lazy Load. Does this mean that, along with the cached object, I still have a long EF6 tail in memory along with an open connection to the database? If so, how to organize caching so that objects that are torn from EF are in the cache?
Thanks for the tip.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan, 2015-03-25
@Razbezhkin

Thank you.
It turns out that there are special settings in the DB context that can completely cut off entities from the context:
here is a piece of code that sets up the context:
CFDB rocontext = new CFDB();
rocontext.Configuration.ProxyCreationEnabled = false;//prohibits the use of proxy classes - we end up with the same types that we defined in the models
rocontext.Configuration.LazyLoadingEnabled = false;//here it is forbidden to use lazy in Include entities that we need, we will get an exception
rocontext.Configuration.ValidateOnSaveEnabled = false; // disables validation when saving, and saving is not implied at all
rocontext.Configuration.AutoDetectChangesEnabled = false;//disables checking for changes when saving, and saving is not implied at all It
seems that this option suits me - nothing but the entities I need remains and they can be safely cached.

Y
Yuri, 2015-03-24
@Gilga

If you mean linked tables, then
SPUserInfo user = _r.SPUserInfo.SingleOrDefault(x => x.UserId == UserId). Include (x =>x.[RelatedTable]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question