Entity framework 4 更新时带有实体框架的OptimisticConcurrencyException影响0行

Entity framework 4 更新时带有实体框架的OptimisticConcurrencyException影响0行,entity-framework-4,Entity Framework 4,我的数据结构如下: SecurityPolicy 1一般来说,这是因为objectstatemanager中实体的时间戳不再与数据库中的时间戳匹配 召唤 coreContext.Refresh(RefreshOptions.StoreWins(或.ClientWins,具体取决于所需内容)、entity) 在调用save之前同步实体和数据库 有关解释乐观并发性的好文章,请参阅 或 一般来说,这是因为objectstatemanager中实体的时间戳与数据库中的时间戳不再匹配 召唤 coreCon

我的数据结构如下:


SecurityPolicy 1一般来说,这是因为objectstatemanager中实体的时间戳不再与数据库中的时间戳匹配

召唤 coreContext.Refresh(RefreshOptions.StoreWins(或.ClientWins,具体取决于所需内容)、entity)

在调用save之前同步实体和数据库

有关解释乐观并发性的好文章,请参阅 或

一般来说,这是因为objectstatemanager中实体的时间戳与数据库中的时间戳不再匹配

召唤 coreContext.Refresh(RefreshOptions.StoreWins(或.ClientWins,具体取决于所需内容)、entity)

在调用save之前同步实体和数据库

有关解释乐观并发性的好文章,请参阅 或

看来我可能误解了Julie Lerman的书,或者需要对她实现存储过程的方式进行一些细微的更改

我已经更改了模型和存储过程,使存储过程返回时间戳,然后模型拾取它。因此,这意味着[Timestamp]字段将不为空

因此,插入存储过程现在看起来像:

create PROCEDURE dbo.sp_M2_Core_InsertSecurityPolicy
    @Name nvarchar(256),
    @Comment nvarchar(max)=null
AS

    declare @nameExists nvarchar(256)
    declare @id int 

    select @nameExists= [Name] from M2_Core_SecurityPolicy where [Name]=@Name
    if (not @nameExists is null)
    begin
        raiserror (N'Name is already in use: %s',
        11,
        1,
        @Name)
    end
    else
    begin

        INSERT INTO M2_Core_SecurityPolicy
            ([Name],Comment)
            values
            (@Name,@Comment)

        IF @@ROWCOUNT > 0 
        BEGIN
            SET @id=SCOPE_IDENTITY()
            SELECT @id as ID,[Timestamp] FROM M2_Core_SecurityPolicy WHERE ID=@id
        END

    end

go
并更改映射,使其拾取“新”字段:


看来我可能误解了Julie Lerman的书,或者需要对她实现存储过程的方式进行一些细微的更改

我已经更改了模型和存储过程,使存储过程返回时间戳,然后模型拾取它。因此,这意味着[Timestamp]字段将不为空

因此,插入存储过程现在看起来像:

create PROCEDURE dbo.sp_M2_Core_InsertSecurityPolicy
    @Name nvarchar(256),
    @Comment nvarchar(max)=null
AS

    declare @nameExists nvarchar(256)
    declare @id int 

    select @nameExists= [Name] from M2_Core_SecurityPolicy where [Name]=@Name
    if (not @nameExists is null)
    begin
        raiserror (N'Name is already in use: %s',
        11,
        1,
        @Name)
    end
    else
    begin

        INSERT INTO M2_Core_SecurityPolicy
            ([Name],Comment)
            values
            (@Name,@Comment)

        IF @@ROWCOUNT > 0 
        BEGIN
            SET @id=SCOPE_IDENTITY()
            SELECT @id as ID,[Timestamp] FROM M2_Core_SecurityPolicy WHERE ID=@id
        END

    end

go
并更改映射,使其拾取“新”字段:


谢谢你,阿马苏里尔。我已经更新了我的问题,包括我的新发现(更新2)。由于我误用了EF,我会发现很难使用Context.Refresh,因为我需要发送需要刷新的确切对象。而且,我想避免这种情况。数据库上没有活动。这是“从记忆中”发生的。谢谢阿马苏里尔。我已经更新了我的问题,包括我的新发现(更新2)。由于我误用了EF,我会发现很难使用Context.Refresh,因为我需要发送需要刷新的确切对象。而且,我想避免这种情况。数据库上没有活动。这是“从记忆中”发生的。
declare @t timestamp
select @t=[timestamp] from M2_Core_SecurityPolicyRule where ID=1
exec [sp_M2_Core_UpdateSecurityPolicyRule] @id=1, @roleName='User',@Rank=2,@Timestamp=@t
create PROCEDURE dbo.sp_M2_Core_InsertSecurityPolicy
    @Name nvarchar(256),
    @Comment nvarchar(max)=null
AS

    declare @nameExists nvarchar(256)
    declare @id int 

    select @nameExists= [Name] from M2_Core_SecurityPolicy where [Name]=@Name
    if (not @nameExists is null)
    begin
        raiserror (N'Name is already in use: %s',
        11,
        1,
        @Name)
    end
    else
    begin

        INSERT INTO M2_Core_SecurityPolicy
            ([Name],Comment)
            values
            (@Name,@Comment)

        IF @@ROWCOUNT > 0 
        BEGIN
            SET @id=SCOPE_IDENTITY()
            SELECT @id as ID,[Timestamp] FROM M2_Core_SecurityPolicy WHERE ID=@id
        END

    end

go