简体   繁体   English

ASP.NET 样板实体立即提交

[英]ASP.NET Boilerplate Entity commit immediately

I've started working with Entities within ASP.NET Boilerplate.我已经开始使用 ASP.NET 样板中的实体。 Im trying to get my entities to commit to the database immediately but i cant seem to do it.我试图让我的实体立即提交到数据库,但我似乎做不到。

I am calling await CreateAsync(EntityDto);我正在调用await CreateAsync(EntityDto); . .

I've tried overriding the CreateAsync within my controller to include CurrentUnitOfWork.SaveChanges();我尝试在 controller 中覆盖 CreateAsync 以包含CurrentUnitOfWork.SaveChanges(); . .

I have also tried to wrap the code in a unit of work.我还尝试将代码包装在一个工作单元中。

        using (var unitOfWork = _unitOfWorkManager.Begin())
        {
            var entityDto = await CreateAsync(entityDto);
            await unitOfWork.CompleteAsync();
        }

But it never seems to commit to the database immediately.但它似乎永远不会立即提交到数据库。

Any help would be much apricated.任何帮助都会非常有用。

According to official document , all you need is to use the SaveChanges or SaveChangesAsync method of the current unit of work.根据官方文档,您只需要使用当前工作单元的SaveChangesSaveChangesAsync方法即可。

Note that if the current unit of work is transactional, all changes in the transaction are rolled back if an exception occurs.请注意,如果当前工作单元是事务性的,那么如果发生异常,事务中的所有更改都会回滚。 Even the saved changes.甚至保存的更改。

ABP's Conventional Unit of Work is scoped around your Controller action and The unit of work is ambient . ABP 的常规工作单元范围围绕您的 Controller 操作,工作单元是环境

You should require a new unit of work with TransactionScopeOption.RequiresNew :您应该使用TransactionScopeOption.RequiresNew需要一个新的工作单元:

using (var unitOfWork = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
{
    var entityDto = await CreateAsync(entityDto);
    await unitOfWork.CompleteAsync();
}

Alternatively, you can suppress the unit of work with TransactionScopeOption.Suppress :或者,您可以使用TransactionScopeOption.Suppress抑制工作单元:

using (var unitOfWork = _unitOfWorkManager.Begin(TransactionScopeOption.Suppress))
{
    var entityDto = await CreateAsync(entityDto);
    await unitOfWork.CompleteAsync();
}

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

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