3

在 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 应用程序中添加了这段代码,以更新我的数据库,并在我的 datagrid 中输入值:

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 的新值,并且我的数据网格显示了我的任务的旧版本。

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

4

1 回答 1

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
               );
            }
        };
    }
}

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

Thanks for your help bye

于 2012-11-28T10:18:01.627 回答