IRepositoy.InsertOrUpdateAsync引发并发异常

时间:2019-04-14 15:33:35

标签: entity-framework asp.net-core asp.net-boilerplate

我正在使用entityframework和.net core在ABP上创建内容管理系统。使用InsertOrUpdateAsync时出现并发异常。我的表中没有任何数据。

请找到用于创建表格的模型。

   [Table("CMSContents")]
   public class CMSContent:Entity<int>
   {
       public const int MAXTITLELENGHT = 128;

       public const int MAXCONTENTLENGTH = 10000;

       [Key]
       [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
       public override int Id { get; set; }
       /// <summary>
       /// The title of the content.
       /// </summary>
       [Required]
       [StringLength(MAXTITLELENGHT)]
       public virtual string PageName { get;  set; }

       /// <summary>
       /// The Cms Content
       /// </summary>
       public virtual  string PageContent { get; set; }

       protected CMSContent()
       {

       }

       public static CMSContent CreateContent(int id,string title ,string contents)
       {
           var @content = new CMSContent
           {
               Id =  id, 
               PageName = title,
               PageContent = contents
           };

           return @content;
       }
   }
}

以下是用于调用存储库的应用程序服务。

public async Task<CMSContent> InsertOrUpdateCMSContent(CreateContentInput input)
        {
            var @content = CMSContent.CreateContent(input.Id,input.PageName, input.PageContent);
            return await _contentManager.InsertOrUpdateAsync(@content);
        }

从Swagger调用此API时发生异常,

Mvc.ExceptionHandling.AbpExceptionFilter-数据库操作应影响1行,但实际上影响0行。自加载实体以来,数据可能已被修改或删除。有关了解和处理乐观并发异常的信息,请参见http://go.microsoft.com/fwlink/?LinkId=527962。 Abp.Domain.Uow.AbpDbConcurrencyException:数据库操作预期会影响1行,但实际上影响0行。自加载实体以来,数据可能已被修改或删除。有关了解和处理乐观并发异常的信息,请参见http://go.microsoft.com/fwlink/?LinkId=527962。 ---> Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:数据库操作预期会影响1行,但实际上影响0行。自加载实体以来,数据可能已被修改或删除。有关了解和处理乐观并发异常的信息,请参见http://go.microsoft.com/fwlink/?LinkId=527962。    在Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32 commandIndex,Int32 ExpectedRowsAffected,Int32 rowsAffected)    在Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetWithoutPropagationAsync(Int32 commandIndex,RelationalDataReader阅读器,CancellationToken cancelledToken)    在Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader阅读器,CancellationToken cancelToken)    在Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection连接,CancellationToken cancelledToken)    在Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(DbContext _,ValueTuple 2 parameters, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func 4操作,Func 4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList 1个条目要保存,CancellationToken cancelledToken)中    在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(Boolean acceptAllChangesOnSuccess,CancellationToken cancelToken)    在Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess,CancellationToken cancelToken)    在D:\ Github \ aspnetboilerplate \ src \ Abp.EntityFrameworkCore \ EntityFrameworkCore \ AbpDbContext.cs:line 224中的Abp.EntityFrameworkCore.AbpDbContext.SaveChangesAsync(CancellationToken cancelleToken)处    ---内部异常堆栈跟踪的结尾---    在Abp.EntityFrameworkCore.AbpDbContext.SaveChangesAsync(CancellationToken cancelleToken)在D:\ Github \ aspnetboilerplate \ src \ Abp.EntityFrameworkCore \ EntityFrameworkCore \ AbpDbContext.cs:line 230    在D:\ Github \ aspnetboilerplate \ src \ Abp.ZeroCore.EntityFrameworkCore \ Zero \ EntityFrameworkCore \ AbpZeroCommonDbContext.cs:line 170中的Abp.Zero.EntityFrameworkCore.AbpZeroCommonDbContext`3.SaveChangesAsync(CancellationToken cancelleToken)中    在D:\ Github \ aspnetboilerplate \ src \ Abp.EntityFrameworkCore \ EntityFrameworkCore \ Uow \ EfCoreUnitOfWork.cs:line 167中的Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.SaveChangesInDbContextAsync(DbContext dbContext)    位于D:\ Github \ aspnetboilerplate \ src \ Abp.EntityFrameworkCore \ EntityFrameworkCore \ Uow \ EfCoreUnitOfWork.cs:Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.SaveChangesAsync()    在D:\ Github \ aspnetboilerplate \ src \ Abp.EntityFrameworkCore \ EntityFrameworkCore \ Uow \ EfCoreUnitOfWork.cs:Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.CompleteUowAsync()    在D:\ Github \ aspnetboilerplate \ src \ Abp \ Domain \ Uow \ UnitOfWorkBase.cs:line 273中的Abp.Domain.Uow.UnitOfWorkBase.CompleteAsync()    在Abp.AspNetCore.Mvc.Uow.AbpUowActionFilter.OnActionExecutionAsync(ActionExecutingContext context,ActionExecutionDelegate next)在D:\ Github \ aspnetboilerplate \ src \ Abp.AspNetCore \ AspNetCore \ Mvc \ Uow \ AbpUowActionFilter.cs:line 51    在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()    在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext上下文)处

1 个答案:

答案 0 :(得分:0)

我尝试了以下方法使其有效,因为评论不允许这么多字符,所以发布它作为答案。

      public async Task<CMSContent> InsertOrUpdateCMSContent(CreateContentInput input)
    {
        var exists = await _contentRepository
            .GetAll()
            .AnyAsync(e => e.Id == input.Id);
        if (!exists)
        {
           var @content = CMSContent.CreateContent(input.PageName, input.PageContent);
            return await _contentRepository.InsertAsync(@content);
        }
        else
        {
            var @content = CMSContent.CreateContent(input.Id, input.PageName, input.PageContent);
            return await _contentRepository.UpdateAsync(@content);
        }            
    }