2

我有一个属性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
4

1 回答 1

0

没有内置方式(尽管您的 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

于 2012-12-06T13:58:42.973 回答