注意:这是基于我的问题here,但这是微妙的不同。我已经解决了另一个问题,特别是关于子实体的问题。
我有一个数据结构:
SecurityPolicy 1< --- * SecurityPolicyRule
因此,SecurityPolicy可以有0个,一个或多个SecurityPolicyRules。
我正在使用Julie Lerman的实体框架书来实现某种程度的并发检查,TDD和POCO支持。
据我所知,每个表都应该有一个rowversion / timestamp字段,标记为ConcurrencyMode == Fixed。
我决定在存储过程中实现CUD。我的UPDATE Sproc如下:
create PROCEDURE dbo.sp_M2_Core_UpdateSecurityPolicy
@ID int,
@Name nvarchar(256),
@Comment nvarchar(max)=null,
@timestamp timestamp
AS
declare @nameExists nvarchar(256)
select @nameExists= [Name] from M2_Core_SecurityPolicy where [Name]=@Name and [ID]<>@id
if (not @nameExists is null)
begin
raiserror (N'Name is already in use: %s',
11,
1,
@Name)
end
else
begin
update M2_Core_SecurityPolicy
set [Name]=@Name,
[Comment]=@Comment
where id=@id and [timestamp]=@timestamp
IF @@ROWCOUNT>0
SELECT [Timestamp] AS newTimeStamp FROM M2_Core_SecurityPolicy WHERE id=@id
end
go
create PROCEDURE dbo.sp_M2_Core_UpdateSecurityPolicyRule
(
@id int,
@RoleName nvarchar(256),
@Rank int,
@CanReadExecute bit=null,
@CanWrite bit=null,
@CanDelete bit=null,
@CanExport bit=null,
@Timestamp timestamp
)
AS
declare @roleExists nvarchar(256)
declare @securityPolicyID int
select @roleExists= [RoleName] from vw_aspnet_Roles where [RoleName]=@RoleName
if (@roleExists is null)
begin
raiserror (N'Role is not defined: %s',
11,
1,
@roleName)
end
else
begin
select @securityPolicyID=[SecurityPolicyID] from M2_Core_SecurityPolicyRule where [id]=@id
-- move all other rules up in priority
IF (SELECT COUNT(*) FROM M2_Core_SecurityPolicyRule WHERE [ID]<>@ID AND [SecurityPolicyID]=@SecurityPolicyID AND [Rank]=@Rank) > 0
BEGIN
UPDATE M2_Core_SecurityPolicyRule
SET [Rank]=[Rank]+1
WHERE [Rank] >= @rank
AND [SecurityPolicyID]=@SecurityPolicyID
AND [ID]<>@ID
END
update M2_Core_SecurityPolicyRule
set [RoleName]=@RoleName,
[Rank]=@Rank,
[CanReadExecute]=@CanReadExecute,
[CanWrite]=@CanWrite,
[CanDelete]=@CanDelete,
[CanExport]=@CanExport
where id=@id and [timestamp]=@timestamp
IF @@ROWCOUNT>0
SELECT [Timestamp] AS newTimeStamp FROM M2_Core_SecurityPolicyRule WHERE id=@id
end
RETURN
go
我正在使用以下代码进行测试:
测试如下:
[TestMethod()]
public void AddWithSecurityPolicyRuleChangeRankTest()
{
ICoreContext coreContext = new CoreEntities(_coreDbConnectionString);
CoreUnitOfWork coreUnitOfWork = new CoreUnitOfWork(coreContext);
SecurityPolicyRepository target = new SecurityPolicyRepository(coreUnitOfWork);
int originalCount = coreContext.SecurityPolicies.Count();
string securityPolicyName = "addwithsecuritypolicyrulechangeruletest";
int originalRank = 1;
SecurityPolicy entity = new SecurityPolicy()
{
Comment = null,
Name = securityPolicyName,
SecurityPolicyRules = new FixUpCollection<SecurityPolicyRule>()
};
entity.SecurityPolicyRules.Add(
new SecurityPolicyRule()
{
CanDelete = null,
CanExport = null,
CanReadExecute = null,
CanWrite = null,
Rank = originalRank,
RoleName = "User"
});
target.Add(entity);
coreUnitOfWork.Save();
entity.SecurityPolicyRules[0].Rank=originalRank+1;
coreUnitOfWork.Save(); // <-- exception thrown here
SecurityPolicy savedSecurityPolicy = target.GetAll().Single(q => q.Name.Equals(securityPolicyName, StringComparison.CurrentCultureIgnoreCase));
Assert.AreEqual(originalRank+1,savedSecurityPolicy.SecurityPolicyRules[0].Rank);
}
然而,当我运行它时,它会在突出显示的行处抛出异常。例外是:
System.Data.OptimisticConcurrencyException 用户代码未处理 Message =存储更新,插入或 删除语句影响了 意外的行数(0)。 实体可能已被修改或 自实体加载后删除。 刷新ObjectStateManager条目 来源= System.Data.Entity的
堆栈跟踪: 在System.Data.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64) rowsAffected,UpdateCommand source) 在System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager) stateManager,IEntityAdapter适配器) 在System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) 在System.Data.Objects.ObjectContext.SaveChanges(SaveOptions 选项) 在System.Data.Objects.ObjectContext.SaveChanges() 在MIGTurbo2.Core.Data.CoreEntities.Save() 在 d:\ dev的\ migturbo2.0 \ MIGTurbo2.Core \数据\ Core.Context.cs:线 92 在MIGTurbo2.Repositories.CoreUnitOfWork.Save() 在 d:\ dev的\ migturbo2.0 \ MIGTurbo2.Repositories \ CoreUnitOfWork.cs:行 26 在MIGTurbo2.Core.Tests.IntegrationTests.SecurityPolicyRepositoryTest.AddWithSecurityPolicyRuleChangeRankTest() 在 d:\ dev的\ migturbo2.0 \ MIGTurbo2.Core.Tests \ IntegrationTests \ SecurityPolicyRepositoryTest.cs:行 524 InnerException:
果然,没有数据发生变化。即。第一次更新时[Rank]仍为1(因此,INSERT)。但是,通过SQL事件探查器和Ayende的EF Profiler运行它,甚至不会调用数据库来进行更新。那么时间戳/ rowversion的相关性肯定是......无关紧要的?
可能导致这种情况的原因是什么?我不想每次保存都刷新数据库!这仅在更新子实体时发生。更改父SecurityPolicy工作正常。
更新1:
运行INSERT代码时,可以很好地创建SecurityPolicyRule。此问题特定于UPDATE和DELETE操作。
更新2:
我已经发现,如果我设置了实体的状态,它就会被提交到数据库。 EF现在已经很好地接收了我的国家财产(使用Julie的书中解释的机制)。因此,在更新项目时,我的测试代码现在看起来像这样:
entity.SecurityPolicyRules[0].Rank=originalRank+1;;
entity.SecurityPolicyRules[0].State = IWW.Elements.State.Modified;
但是,尽管发送到数据库的调用和参数存在且正确,但它仍然无法正常工作。有趣的是,如下面的屏幕所示,最后一行显示在异常之后。虽然我确信这只是实体框架配置文件“刷新”其监视器。
子实体的存储过程是否返回正确的数据?