Base solution for your next web application
Open Closed

Current.SaveChanges() how to insert element #1366


User avatar
0
hans abelshausen created

"If current unit of work is transactional, all changes in the transaction are rolled back if an exception occurs, even saved changes." We are getting an out of memory exception inserting elements. So our plan is to save the elements before memory exception occurs, but everything is rolled back. Is there a way to insert the elements although memory exception appears.


8 Answer(s)
  • User Avatar
    0
    hans abelshausen created

    Okay, I think [UnitOfWork(isTransactional:false)] could be the solution..

  • User Avatar
    0
    mmax created

    I have a similar problem. I need to insert and immediately save changes to the db. Simplest code example:

    public ActionResult TestAction()
    {
        using (var context = new TestDbContext())
        {
            context.TestDbSet.Add(new TestInst {...});
            context.SaveChanges();
        }
    
        // should be in the db now, but it's still local
    
        return Content("Ok");
    }
    

    In some reason it's not saving data to db after save changes was called. It's saving only after action was totally executed and page loaded. I'm tried to do the same using repository and unit of work manager, but the result is the same. Tried to add UnitOfWork attribute. Not working. How can I force save data to db? Please help! Many thanks!

  • User Avatar
    0
    hikalkan created
    Support Team

    You should not create dbcontext directly, always use dependency injection. But.. in ABP you also should not inject dbcontext directly, but inject and use repository. Anyway, see this to save changes immediately: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#DocUowSaveChanges">http://www.aspnetboilerplate.com/Pages/ ... aveChanges</a>

    CurrentUnitOfWork.SaveChanges();

  • User Avatar
    0
    mmax created

    <cite>hikalkan: </cite> You should not create dbcontext directly, always use dependency injection. But.. in ABP you also should not inject dbcontext directly, but inject and use repository. Anyway, see this to save changes immediately: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#DocUowSaveChanges">http://www.aspnetboilerplate.com/Pages/ ... aveChanges</a>

    CurrentUnitOfWork.SaveChanges();

    I know all that and just made as simple as possible demonstration of what I'm trying to do. In a real project I have a custom repository with quite a lot of custom methods. The call of repository is locating in business logic, not in the controller... But even this super simple code is not working as I'm expecting. Ok, I tried one more simple test code using repositories and it's working the same. No immediate save, only after action is completely proccessed...

    Is this logic works for you? What am I doing wrong? What else can I try?

    private readonly IRepository<TestEntity> _testRepository;
    public Constructor(IRepository<TestEntity> testRepository)
    {	
    	_testRepository = testRepository;
    }
    
    public async Task<ActionResult> TestAction()
    {
    	await _testRepository.InsertAsync(new TestEntity {...});
    	await CurrentUnitOfWork.SaveChangesAsync();
    
    	// nothing in db
    	return Content("Done");
    }
    
  • User Avatar
    0
    mmax created

    So, I need to commit the transaction on SaveChanges or after method executed (not action).

    Tried to use UnitOfWork attribute and UnitOfWorkManager. Result is the same.

  • User Avatar
    0
    mmax created

    Forgot to say, I'm using the latest Abp 1.0. Tried older versions. The same problem.

  • User Avatar
    0
    mmax created

    Please help me somebody!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you try to do it like this to see if it is saved or not. Check the value of Id after SaveChangesAsync called.

    var test = new TestEntity {...};
     await _testRepository.InsertAsync(test);
     await CurrentUnitOfWork.SaveChangesAsync();
     var id = test.Id;
    

    Since the Action is transactional, the record is inserted to the database but you cannot see it until the transaction completes which is not related to ABP.