【问题标题】:In EF-CF is it possible to fetch once then hold in memory?在 EF-CF 中是否可以获取一次然后保存在内存中?
【发布时间】:2012-11-24 12:58:49
【问题描述】:

我有一个属性LeadTypeID,如果为 null,我希望 EF 获取该属性,然后在应用程序的生命周期中保存在内存中。有没有内置的方法可以做到这一点,或者有人有建议?谢谢

Public Class Lead
    Public Property LeadID As Integer
    Public Property Name As String
    Public Property LeadTypeID As Integer
    Public Overridable Property LeadType As LeadType
End Class

Public Class LeadType
    Public Property LeadTypeID As Integer
    Public Property Name As String
    Public Property LastUpdated As Date
End Class

【问题讨论】:

    标签: entity-framework entity-framework-4 entity-framework-4.1


    【解决方案1】:

    没有内置方式(尽管您的 Lead 实体将在上下文的整个生命周期内保留在本地图中,但在应用程序的生命周期内保留上下文并不是一个好主意)。

    我个人使用依赖注入和一个小缓存对象来解决这个问题。以下是我如何缓存整个表(使用直写缓存)的示例,缓存单个整数更简单,在构造函数中注入/创建上下文,将值设置为公共属性,并将其绑定到单例中范围。

    缓存对象: https://github.com/lukemcgregor/StaticVoid.Blog/blob/master/Blog/Data/Entities/Blog/CachedBlogRepository.cs

    绑定: https://github.com/lukemcgregor/StaticVoid.Blog/blob/master/Blog/Data/PersistanceModule.cs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      相关资源
      最近更新 更多