使用EF执行SP时,对象不会刷新

时间:2014-12-12 06:01:20

标签: sql-server vb.net entity-framework stored-procedures

我正在运行一个更新记录然后返回更新记录的SP。返回的记录表明没有进行任何更改,但如果我直接查看数据库中的记录,它确实已更新。选择该记录的任何后续SP都将返回记录的原始版本。

存储过程:

[POSeries_UpdateWins]
    -- Add the parameters for the stored procedure here
@POSeriesID int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    DECLARE @Team1 int
    DECLARE @Team2 int
    DECLARE @Team1Wins int
    DECLARE @Team2Wins int
    SELECT @Team1 = Team1ID, @Team2 = Team2ID
    FROM POSeries 
    WHERE POSeriesID = @POSeriesID

    DECLARE @Temp int
    SELECT @Temp = home
    FROM Results
    WHERE POSeriesID = @POSeriesID AND ((HomeID = @Team1 AND Home> Visitor) OR (VisitorID = @Team1 AND Visitor > Home))

    SELECT @Team1Wins = @@ROWCOUNT

    SELECT @Temp = home
    FROM Results
    WHERE POSeriesID = @POSeriesID AND ((HomeID = @Team2 AND Home> Visitor) OR (VisitorID = @Team2 AND Visitor > Home))

    SELECT @Team2Wins = @@ROWCOUNT

    UPDATE POSeries
    set Team1Wins = @Team1Wins, Team2Wins = @Team2Wins
    WHERE POSeriesID = @POSeriesID

    SELECT POSeriesID, SeasonID, POSeasonID, POBracketsID, Series, Team1ID, Team1Wins, Team1Rank, Team2ID, Team2Wins, Team2Rank, Round, Games, WinnerPlaysID, isComplete
    FROM POSeries
    WHERE POSeriesID = @POSeriesID

调用导入EF并设置为返回POSeries实体的SP的方法

Public Function POSeries_UpdateWins(ByVal mID As Integer) As POSeries

        Dim mPOSerieses As ObjectResult(Of POSeries) = mData.POSeries_UpdateWins(mID)
        Dim mPOSeries As POSeries = mPOSerieses.FirstOrDefault
        Return mPOSeries
    End Function

处理此内容的代码段:

    Dim oPOSeries As List(Of POSeries) = mEDF.POSeries_SelByPOSeasonID(mSeasonID)
    For Each pos As POSeries In oPOSeries
        If pos.isComplete = False Then
            pos = mEDF.POSeries_UpdateWins(pos.POSeriesID)
            pos = mEDF.POSeries_SelByID(pos.POSeriesID)
            If pos.Team1Wins > (pos.Games / 2) Or pos.Team2Wins > (pos.Games / 2) Then
                pos.isComplete = True
                mEDF.POSeries_Save(pos)
            End If
        End If
    Next

所以基本上当调用mEDF.POSeries_UpdateWins时,它返回对象pos不变。但如果我进入SSMS并运行

SELECT * ON POSeries WHERE POSeries = ID 

它显示Team1Wins和Team2Wins正确更新。即使在事实之后运行mEDF.POSeries_SelByID仍然会返回未更改的记录。

我做错了什么?不应该在EF中运行的初始SP返回更正后的记录吗?

1 个答案:

答案 0 :(得分:0)

使用SET NOCOUNT ON,您将无法在@@ ROWCOUNT中获得正确的结果。

http://blog.sqlauthority.com/2011/12/02/sql-server-effect-of-set-no-count-on-rowcount/