如何使用数据库优先方法刷新DbContext

时间:2019-01-17 15:18:59

标签: c# entity-framework mvvm

问题:我们有一个潜在的多用户环境应用程序。由于实体的缓存,当数据库发生更改之后/之后,我们无法看到其他用户在数据库中所做的更改。我不希望持续轮询数据库。如果用户可以单击“刷新”按钮并让上下文从数据库中获取新信息,我会没事的。

注意事项: -我们正在使用具有相当长使用寿命的缓存,并希望保留该缓存。 -独家寻找一种使用数据库中的最新值刷新上下文的方法。

任何见识将不胜感激。我可以根据要求提供代码示例。

谢谢

2 个答案:

答案 0 :(得分:1)

如果要从数据库中重新加载单个实体,可以执行以下操作:

CREATE TRIGGER [dbo].[T_PDF_WV_RESULT] ON [dbo].[T_PDF] 
AFTER INSERT 
AS  
BEGIN

INSERT T_WV_RESULT (ID , NAME, SFMTCODE, SFMT, 
    LANGCODE, LANG, PLANTCODE, FMTCODE,
    RDATE, CASNUM,  TN, CP,
    CUS5, MANU, SYN, ICO,
    DOC, COVER, [GUID], SDS, 
    SITECODE, PDATE, CASDATA, CUS3
    )
    SELECT TP.F_PRODUCT, TP.F_PRODUCT_NAME, TP.F_SUBFORMAT, (SELECT MS.F_MSDSTYPE_DESC FROM T_MSDSTYPES MS WHERE MS.F_MSDSTYPE = TP.F_SUBFORMAT AND MS.F_FORMAT = TP.F_FORMAT) AS SFMT,
    TP.F_LANGUAGE, (SELECT TL.F_LANGUAGE_NAME FROM T_LANGUAGES TL WHERE TL.F_LANGUAGE = TP.F_LANGUAGE) AS LANG, TP.F_PLANT AS PLANTCODE, TP.F_FORMAT AS FMTCODE,
    TP.F_DATE_REVISED, F_CAS_NUMBERS, TP.F_CUSTOM1, TP.F_CUSTOM2,
    (SELECT TC.F_COUNTRY_NAME FROM T_COUNTRIES TC WHERE TC.F_COUNTRY_CODE = TP.F_CUSTOM5) AS cus5,
    (SELECT F_PHRASE FROM [V_PROD_ALIAS_MANU] WHERE F_PRODUCT = TP.F_PRODUCT) AS MANU, 
    (SELECT F_DATA FROM V_PROD_ALIAS_SYN WHERE F_PRODUCT = TP.F_PRODUCT) AS SYN, 
    (SELECT F_DATA FROM V_PROD_ALIAS_ICO WHERE F_PRODUCT = TP.F_PRODUCT) AS ICO, 
    'SDS - ' + (SELECT TL.F_LANGUAGE_NAME FROM T_LANGUAGES TL WHERE TL.F_LANGUAGE = TP.F_LANGUAGE) + ' - PDF' AS DOC, '' AS COVER, CAST(TP.F_GUID AS VARCHAR(36)) + '_PDF' AS [GUID], 'PDF' AS SDS,
    (SELECT TOP 1 F_TEXT_CODE FROM V_PROD_ALIAS_SITE WHERE F_PRODUCT = TP.F_PRODUCT) AS SITECODE,
     TP.F_PUBLISHED_DATE, (SELECT F_DATA FROM V_PROD_ALIAS_CASN WHERE F_PRODUCT = TP.F_PRODUCT) AS CASDATA, TP.F_CUSTOM3 AS CUS3
    FROM INSERTED TP
    WHERE TP.F_AUTHORIZED IN (0,3)

答案 1 :(得分:0)

您可以执行以下操作:

_dbContext.ChangeTracker.Entries().Where(e => e.Entity != null).ToList()
            .ForEach(e => e.State = EntityState.Detached);