How to make Entity Framework cache some objects

后端 未结 3 1830
执笔经年
执笔经年 2020-12-07 15:43

I\'m using Entity Framework.

In my database are stored persons and country of their origins. When I\'m loading a person object I wan\'t to know what is the place eac

3条回答
  •  时光说笑
    2020-12-07 16:20

    As Luhmann has already said my mate Jarek has an excellent sample Caching provider for the Entity Framework. Which he talks about here.

    However sometimes this approach is overkill, and I suspect that caching countries is a good example where the effort isn't worth it. It maybe better to implement some sort of ad-hoc caching.

    If you try to do this the main problem you will run into is that an instance Entity Framework entity can only be attached to one ObjectContext at a time, which means if you are getting concurrent requests (like in a website) you will have problems is you just grab items from the cache and use them blindly.

    What you need is a cloning cache, i.e. so when you grab something from the cache it clones them.

    I put together a tip that illustrates how to do this Tip 14 - How to cache Entity Framework Reference Data

    Hope this helps

    Alex James

    Entity Framework Team Microsoft.

提交回复
热议问题