使用存储过程修改数据库后更新上下文

时间:2012-11-27 11:04:35

标签: wcf c#-4.0 silverlight-4.0 wcf-ria-services

在Silverlight应用程序中,我使用存储过程来更新数据库的表。

ALTER PROCEDURE  [dbo].[SP_ADD_MIS_MISSION] 
(
    @IdMission as int,
    @IdVersion as smallint,
    @LibMission as varchar (50)
)
AS
BEGIN
DECLARE @IDNEWVERSION as int = 1,
        @IDNEWMISSION as int = @IdMission,
        @supp as bit = 1
BEGIN
        IF @IdMission != 0 AND @IdMission is not NULL
        BEGIN
            SELECT @IDNEWVERSION = MAX(IDVERSION)+1 FROM MIS_Mission where MIS_Mission.IdMission=@IdMission

            Update MIS_Mission SET Suppression = @supp WHERE MIS_Mission.IdMission=@IdMission AND MIS_Mission.IdVersion=(@IDNEWVERSION-1)
        END
        ELSE
        BEGIN
            SET @IDNEWVERSION = 1
            select @IDNEWMISSION = MAX(MIS_Mission.IdMission)+1 from MIS_Mission

            if @IDNEWMISSION is NULL SET @IDNEWMISSION=1
        END

        Insert MIS_Mission ( IdMission,
                             IdVersion,
                             LibMission,
                             Suppression
                           )

                    values ( @IDNEWMISSION,
                             @IDNEWVERSION,
                             @LibMission,
                             @Suppression
                           )
        SELECT @IDNEWMISSION
END

我在WCF RIA服务中添加了此代码以启动存储过程:

public void SetMission(MIS_Mission mis)
{
    _entity.ADD_MIS_MISSION(mis.IdMission,mis.IdVersion,mis.LibMission);
}

我在Silverlight应用程序中添加了此代码,以便在我的数据网格中输入值时更新我的​​数据库:

private void UpdateMission_Click(object sender, RoutedEventArgs e)
{

    foreach(MIS_Mission mis in dG_Mission.ItemsSource)
    {
        var operation = _Context.SetMission(mis);

        operation.Completed += (se, ev) =>
        {
        };
    }
}

但是当我完成更新我的数据库时,我的上下文没有更新IdVersion的新值,而我的数据网格显示我的任务的旧版本。

如何在不重新加载数据网格的情况下更新我的上下文?

1 个答案:

答案 0 :(得分:0)

我在这个网站上找到了一个解决方案:http://weblogs.asp.net/fredriknormen/archive/2009/11/24/refresh-the-cached-entityset-after-a-submitchanges-wcf-ria-services.aspx

我的解决方案是这样的:

private void UpdateMission_Click(object sender, RoutedEventArgs e)
{
    int countenr = itemsSource.Count;

    foreach(MIS_Mission mis in dG_Mission.ItemsSource)
    {
        var operation = _Context.SetMission(mis);

        operation.Completed += (se, ev) =>
        {
            countenr--;
            if (countenr == 0)
            {
                _Context.Load<MIS_Mission>(
                    _Context.ListMissionQuery(),
                    LoadBehavior.RefreshCurrent,
                    loadOperation =>
                    {
                       if (loadOperation.HasError)
                       {
                           MessageBox.Show(loadOperation.Error.Message);
                           loadOperation.MarkErrorAsHandled();
                       }
                       else
                       {
                           List<MIS_Mission> itemsource = dG_Mission.ItemsSource as List<MIS_Mission>;
                           var results = itemsource.Where(entity => !loadOperation.Entities.Contains(entity)).ToList();

                           results.ForEach(entity => itemsource.Remove(entity));

                           results = loadOperation.Entities.Where(entity => !itemsource.Contains(entity)).ToList();

                           results.ForEach(entity => itemsource.Add(entity));

                       }
                   }
                   ,null
               );
            }
        };
    }
}

这个解决方案对我来说很好,但数据网格的刷新有点慢。

感谢您的帮助 再见