简体   繁体   English

EF Core Savechanges 不适用于删除

[英]EF Core Savechanges not working for Remove

I am trying to remove child item(s) from my domain, but Savechanges() is not working and no exceptions occurred and I am tracing and find my entity in Dbcontext that state changed to modified.我正在尝试从我的域中删除子项,但 Savechanges() 不起作用并且没有发生异常,我正在跟踪并在 Dbcontext 中找到我的实体,该实体将 state 更改为已修改。 add or update working correctly.添加或更新正常工作。 everything worked ok before I add IEventbus interface to publish an event.在我添加 IEventbus 接口以发布事件之前,一切正常。 my remove method in the domain class.我在域 class 中的删除方法。

 public void RemoveItem(Guid id, IEventBus eventBus)
    {
        if (!string.IsNullOrWhiteSpace(this.Confirmer.UserId))
        {
            throw new RemoveItemOfConfirmedScrapException();
        }

        var scrapItem = this.ScrapItems.First(p => p.Id == id);

        var assetId = scrapItem.AssetId;


        this.ScrapItems.Remove(this.ScrapItems.First(p => p.Id == id));


          eventBus.Publish(new ScrapItemRemovedEvent(assetId));
    }

and this is my UnitOfWork for saving in the database.这是我用于保存在数据库中的 UnitOfWork。

 public class UnitOfWork : IUnitWork
{
    private readonly IDbContext dbContext;

    private DbContextBase dbContextBase;
    public UnitOfWork(IDbContext dbContext)
    {
        this.dbContext = dbContext;

        this.dbContextBase = dbContext as DbContextBase;
    }

    public void Commit()
    {
        try
        {
            this.dbContext.SaveChanges();
        }
        catch
        {

            RollBack();
            throw;
        }

    }
    public void RollBack()
    {
        this.dbContextBase.ChangeTracker.Entries()
            .Where(e => e.Entity != null).ToList()
            .ForEach(e => e.State = EntityState.Detached);
    }
}

what happens to that delete works in other domains even in parent entity but in this case not delete in database.即使在父实体中,该删除也会在其他域中起作用,但在这种情况下不会在数据库中删除。 and ChangeTracker shows that specific entity state changed to "modified" but without any exception finish job and not effected on the database.并且 ChangeTracker 显示特定实体 state 更改为“已修改”,但没有任何异常完成作业并且不会影响数据库。

I have an application service layer with decoration design pattern to call UnitOfWork我有一个带有装饰设计模式的应用程序服务层来调用 UnitOfWork

     public void Dispatch<TCommand>(TCommand command)
        where TCommand : Command
    {
        var commandHandler = this.diContainer.Resolve<ICommandHandler<TCommand>>();
        var transactionCommandHandler = new TransactionalCommandHandler<TCommand>(commandHandler, this.diContainer);
        var logCommandHandler = new LogCommandHandler<TCommand>(transactionCommandHandler);
        logCommandHandler.Execute(command);
    }

and transactional class that calls UnitOfWork和调用 UnitOfWork 的事务性 class

    public class TransactionalCommandHandler<TCommand> : ICommandHandler<TCommand>
    where TCommand : Command
{
    private readonly ICommandHandler<TCommand> commandHandler;

    private readonly IDiContainer container;

    public TransactionalCommandHandler(ICommandHandler<TCommand> commandHandler, IDiContainer container)
    {
        this.commandHandler = commandHandler;
        this.container = container;
    }

    public void Execute(TCommand command)
    {
        var unitOfWork = this.container.Resolve<IUnitWork>(); // ServiceLocator.Current.Resolve<IUnitWork>();

        try
        {
            this.commandHandler.Execute(command);
            unitOfWork.Commit();
        }
        catch (Exception e)
        {
           unitOfWork.RollBack();
            throw;
        }


    }
}
    var scrapItem = this.ScrapItems.First(p => p.Id == id);
    var assetId = scrapItem.AssetId;
    this.ScrapItems.Remove(scrapItem);
    this.Commit(); // you weren't committing before

Removing entity from collection only marks it's state as Deleted.从集合中删除实体仅将其 state 标记为已删除。 You need to call SaveChanges method in order to reflect the changes to database.您需要调用 SaveChanges 方法以反映对数据库的更改。

A very simple carelessness had caused this problem.一个非常简单的疏忽导致了这个问题。 in my command facade have transaction scope that forgot put "scope.complete()"在我的命令门面有事务 scope 忘记放“scope.complete()”

    public void DeleteScrapItem(DeleteScrapItemCommand command)
    {

        using (var scope = new TransactionScope())
        {
            Action<dynamic> updateAssetStatus = p => this.CommandBus.Dispatch(new UpdateAssetStatusCommand()
            {
                AssetId = p.AssetId,
                Status = 0
            });

            this.eventBus.Subscribe<ScrapItemRemovedEvent>(updateAssetStatus);

            this.CommandBus.Dispatch(command);

            scope.Complete(); // forgot put this ...
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM