无法在一个事务中使用两个EF存储库插入两个一对一实体(方法挂起) [英] Cannot insert two one-to-one entities using two EF repositories in one transaction (method hangs)

查看:143
本文介绍了无法在一个事务中使用两个EF存储库插入两个一对一实体(方法挂起)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个通过外键一对一绑定的实体: CreateTenantDto SaasTenantCreateDto . 我需要使用两个存储库(_abpTenantRepository ABP Framework 的第三方存储库的实例)将这些实体插入数据库.我正在尝试为此使用 ABP UnitOfWork 实现.插入 SaasTenantCreateDto 实体后,我试图插入依赖于它的 CreateTenantDto 条目.如果我使用OnCompleted事件插入 CreateTenantDto 记录-该方法在返回newTenantDto之前未输入OnCompleted,而后者又作为null返回(最终插入了记录,但是如果插入成功,我想返回插入的实体).如果我根本不使用OnCompleted-该方法将挂起(看起来像数据库锁).如果我使用两个嵌套的 UnitOfWork 对象-该方法也会挂起.如果我将范围用于两个存储库-

I have two entities bound as one-to-one via foreignkey: CreateTenantDto and SaasTenantCreateDto. I need to use TWO repositories (_abpTenantRepository is an instance of 3rd party repository from ABP Framework) to insert those entities into DB. I am trying to use ABP UnitOfWork implementation for this. After SaasTenantCreateDto entity is inserted, I am trying to insert CreateTenantDto entry which depends on it. If I use OnCompleted event to insert a CreateTenantDto record - the method does not enter OnCompleted before returning newTenantDto and the latter is returned as a null (the records are inserted finally, but I want to return the inserted entity if it's inserted successfully). If I don't use OnCompleted at all - the method hangs (looks like DB lock). If I use two nested UnitOfWork objects - the method hangs as well. If I use the scope for working with two repositories -

    using (var scope = ServiceProvider.CreateScope())
    {
            var unitOfWorkManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
            using (var tenantUow = unitOfWorkManager.Begin(new AbpUnitOfWorkOptions { IsTransactional = true }))
            { ... }
    }

它也挂起...绝对是锁,它与从新创建的newAbpTenant访问ID有关:我可以在SQL Developer Sessions中看到它

it hangs also... It is definitely the lock and it has to do with accessing the id from the newly created newAbpTenant: I can see that in SQL Developer Sessions

enq:TX-行锁争用

enq: TX - row lock contention

有罪的会话是我的HttpApi主机会话中的另一个.原因可能是因为Oracle文档所说:尽管子表上的INSERT,UPDATE和DELETE语句等待父表的索引上的行锁,但子表上的INSERT,UPDATE和DELETE语句未获得任何锁.要清除的表格." -SaveChangesAsync是否导致新的记录行锁定?

and guilty session is another my HttpApi host session. Probably, the reason is as Oracle doc says: "INSERT, UPDATE, and DELETE statements on the child table do not acquire any locks on the parent table, although INSERT and UPDATE statements wait for a row-lock on the index of the parent table to clear." - SaveChangesAsync causes new record row lock?

如何解决此问题?

    //OnModelCreatingBinding
    builder.Entity<Tenant>()
            .HasOne(x => x.AbpTenant)
            .WithOne()
            .HasPrincipalKey<Volo.Saas.Tenant>(x => x.Id)
            .HasForeignKey<Tenant>(x => x.AbpId);
    ...
    b.Property(x => x.AbpId).HasColumnName("C_ABP_TENANT").IsRequired();

    //Mapping ignoration to avoid problems with 'bound' entities, since using separate repositories for Insert / Update
    CreateMap<CreateTenantDto, Tenant>().ForMember(x => x.AbpTenant, opt => opt.Ignore());
    CreateMap<UpdateTenantDto, Tenant>().ForMember(x => x.AbpTenant, opt => opt.Ignore());

    public class CreateTenantDto
    {
        [Required]
        public int Id { get; set; }

        ...

        public Guid? AbpId { get; set; }

        public SaasTenantCreateDto AbpTenant { get; set; }
    }

    public async Task<TenantDto> CreateAsync(CreateTenantDto input)
    {
        try
        {
            TenantDto newTenantDto = null;
            using (var uow = _unitOfWorkManager.Begin(new AbpUnitOfWorkOptions { IsTransactional = true, IsolationLevel = System.Data.IsolationLevel.Serializable }))
            {
                var abpTenant = await _abpTenantManager.CreateAsync(input.AbpTenant.Name, input.AbpTenant.EditionId);
                input.AbpTenant.MapExtraPropertiesTo(abpTenant);
                var newAbpTenant = await _abpTenantRepository.InsertAsync(abpTenant);
                await uow.SaveChangesAsync();

                var tenant = ObjectMapper.Map<CreateTenantDto, Tenant>(input);
                tenant.AbpId = newAbpTenant.Id;
                var newTenant = await _tenantRepository.InsertAsync(tenant);
                newTenantDto = ObjectMapper.Map<Tenant, TenantDto>(newTenant);

                await uow.CompleteAsync();
            }

            return newTenantDto;
    }

    //Implementation by ABP Framework
    public virtual async Task CompleteAsync(CancellationToken cancellationToken = default)
    {
        if (_isRolledback)
        {
            return;
        }

        PreventMultipleComplete();

        try
        {
            _isCompleting = true;
            await SaveChangesAsync(cancellationToken);
            await CommitTransactionsAsync();
            IsCompleted = true;
            await OnCompletedAsync();
        }
        catch (Exception ex)
        {
            _exception = ex;
            throw;
        }
    }

推荐答案

我终于使用以下方法解决了这个问题(但是它没有使用两个似乎无法实现的存储库,因为我们需要直接操作DbContext ):

I have finally resolved the problem using the following approach (but it is not using TWO repositories which seems to be impossible to implement, since we need to manipulate DbContext directly):

应用程序服务层:

            //requiresNew: true - to be able to use TransactionScope
            //isTransactional: false, otherwise it won't be possible to use TransactionScope, since we would have active ambient transaction

            using var uow = _unitOfWorkManager.Begin(requiresNew: true);
            var abpTenant = await _abpTenantManager.CreateAsync(input.AbpTenant.Name, input.AbpTenant.EditionId);
            input.AbpTenant.MapExtraPropertiesTo(abpTenant);
            var tenant = ObjectMapper.Map<CreateTenantDto, Tenant>(input);
            var newTenant = await _tenantRepository.InsertAsync(tenant, abpTenant);
            await uow.CompleteAsync();
            return ObjectMapper.Map<Tenant, TenantDto>(newTenant);

存储库(EntityFrameworkCore)层上的手工InsertAsync方法:

Handmade InsertAsync method on Repository (EntityFrameworkCore) layer:

        using (new TransactionScope(asyncFlowOption: TransactionScopeAsyncFlowOption.Enabled))
        {
            var newAbpTenant = DbContext.AbpTenants.Add(abpTenant).Entity;
            tenant.AbpId = newAbpTenant.Id;
            var newTenant = DbContext.Tenants.Add(tenant).Entity;
            if (autoSave)
            {
                await DbContext.SaveChangesAsync(GetCancellationToken(cancellationToken));
            }
            return newTenant;
        }

这篇关于无法在一个事务中使用两个EF存储库插入两个一对一实体(方法挂起)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆