Base solution for your next web application
Open Closed

Kod transaction'ı bitmeden kayıdın tekrar databaseden sorgulanması #7848


User avatar
0
erkateknoloji created

Merhaba,

xyzRepository.Insert(xyz); yaptıktan sonra, bu satırın hemen altında xyzRepository.GetAll().Where(x=>x...); yaparak, kayıdın son halini databaseden sorgulamak istiyorum fakat kod blokları tamamlanmadığı için bu kayıdın fiziksel halini database'de bulamamış oluyorum.

[UnitOfWork(isTransactional: false)] using (var unitOfWork = _unitOfWorkManager.Begin())

yukarıda yöntemleri de denedim olmadı.

özetle, ben xyzRepository.Insert(xyz); satırı çalıştıktan sonra kayıdın anında database'de olmasını istiyorum.

Teşekkürler.


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Can you explain your problem in English? If you can, please share some code...

    Note that ORM frameworks like NHibernate and EntityFramework internally save changes in a single command. Assume that you updated a few Entities in a non-transactional UOW. Even in this situation all the updates are performed at end of the unit of work with a single database command. If you execute an SQL query directly, it's performed immediately and not rolled back if your UOW is not transactional.

    There is a restriction for non-transactional UOWs. If you're already in a transactional unit of work scope, setting isTransactional to false is ignored (use the Transaction Scope Option to create a non-transactional unit of work in a transactional unit of work).

    Use a non-transactional unit of work carefully since most of the time things should be transactional to ensure data integrity. If your method just reads data and does not change it, it can be safely non-transactional.

  • User Avatar
    1
    yekalkan created

    xyzRepository.Insert(xyz); await CurrentUnitOfWork.SaveChangesAsync(); xyzRepository.GetAll().Where(x=>x...);

  • User Avatar
    0
    erkateknoloji created

    Previously answered in Turkish. I'm sorry. I insert data to database. But, I see data in database when transaction code end. But, I want to see before the code ends.

          public virtual async Task Create(CreateOrEditAirplaneTicketDto input)
            {
                var airplaneTicket = ObjectMapper.Map<AirplaneTicket>(input);
    
    
                if (AbpSession.TenantId != null)
                {
                    airplaneTicket.TenantId = (int?)AbpSession.TenantId;
                }
    
                _airplaneTicketRepository.Insert(airplaneTicket);
                
                 **var result = _airplaneTicketRepository.GetAll();**
                
            }
    

    Why result object is null?

    Thanks.

  • User Avatar
    0
    erkateknoloji created

    This code worked. And thank you @yekalkan and @maliming

            public virtual async Task Create(CreateOrEditAirplaneTicketDto input)
            {
                var airplaneTicket = ObjectMapper.Map<AirplaneTicket>(input);
    
    
                if (AbpSession.TenantId != null)
                {
                    airplaneTicket.TenantId = (int?)AbpSession.TenantId;
    
                }
    
                using (var unitofwork = _unitOfWorkManager.Begin())
                {
                    await _airplaneTicketRepository.InsertAsync(airplaneTicket);
                    await _unitOfWorkManager.Current.SaveChangesAsync();
                    unitofwork.Complete();
                    unitofwork.Dispose();
                }
    
                var result = _airplaneTicketRepository.GetAll();
            }