实体框架:当使用函数导入更新并在存储过程中重新选择实体时,实体被缓存 [英] Entity Framework: Entity is cached when update and reselecting it in a stored procedure using a function import

查看:89
本文介绍了实体框架:当使用函数导入更新并在存储过程中重新选择实体时,实体被缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2010和SQL Server 2008 R2中使用EF4。

I'm using EF4 with Visual Studio 2010 and SQL Server 2008 R2.

我正在选择一个实体。之后,我调用更新该实体的存储过程,从数据库重新选择该实体。当我在代码中捕获存储过程的结果时,我看到旧的(以前选择的)属性。

I am selecting an entity. After that I call a stored procedure which updates that entity en reselects it from the database. When I catch the result of the stored procedure in code, I see that the old (previously selected) properties.

显然我正在查看缓存的实体值。有没有办法来证明我的实体更新了EF?或者是某种魔法属性?

Obviously I'm looking at the cached entity value. Is there a way to signal EF that my entity was updated? Or a magic property of some sort?

我的数据库表(和实体)看起来像这样:

My database table (and entity) look something like this:

CREATE TABLE [Message]
(
    ID int IDENTITY(1, 1) PRIMARY KEY,
    Content XML,
    StateID int NOT NULL,
)

我的SP是这样的:

CREATE PROCEDURE sp_Queue_s
AS
BEGIN
    DECLARE @queue table ([ID] int NOT NULL)
    BEGIN TRAN
        INSERT INTO @queue
        SELECT [ID]
        FROM [Message]
        WHERE   StateID = 1

        UPDATE [Message]
        SET StateID = 2
        WHERE ID IN (SELECT ID FROM @queue)
    COMMIT TRAN
    -- Select the queue
    SELECT [ID], [Content], [Message]
    FROM [Message]
    WHERE [ID] IN (SELECT ID FROM @queue)
END

我的C#代码看起来像这样:

My C# code looks something like this:

using (var context = new MyEntities())
            {
                int id = 1;
                var message = context.Messages.Single(m => m.ID == id);
                var messages = context.GetQueue(); // Function import of sp_Queue_s, maps on the Message entity
                var messageUpdated = messages.Single(m => m.ID == id);
            }


推荐答案

GetQueue 应该是内部调用 context.ExecuteFunction 的生成方法。 ExecuteFunction 有多个重载,其中一个接受 MergeOption 。尝试使用 MergeOption.OverwriteChanges 直接调用 ExecuteFunction

GetQueue should be a generated method internally calling context.ExecuteFunction. ExecuteFunction has multiple overloads and one of them accepts MergeOption. Try to call ExecuteFunction directly with MergeOption.OverwriteChanges.

这篇关于实体框架:当使用函数导入更新并在存储过程中重新选择实体时,实体被缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆