Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

_repository.InsertAsync return id=-2147482647 #4170


User avatar
0
fguo created

When I use DbContext directly to insert an object to an entity, the new inserted object id can be used immediately after SaveChanges(). For example,

MyContext.MyEntity.AddObject(obj); MyContext.SaveChanges(); var id=obj.id; // It is new id.

When I use await _repository.InsertAsync(obj), It returns the same type of "obj" but with id = -2147482647, instead of the new inserted object. Is it by design or a bug?


7 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    It is by design. InsertAsync only does Table.Add(entity), which avoids the overhead of calling SaveChanges.

    You can use InsertAndGetIdAsync or call SaveChanges:

    // 1
    var id = await _repository.InsertAndGetIdAsync(obj); // New id
    
    // 2
    await _repository.InsertAsync(obj);
    _unitOfWorkManager.Current.SaveChanges();
    var id = obj.Id; // New id
    
  • User Avatar
    0
    fguo created

    Understand. I just feel the "-2147482647" weird.

    Thank you for clarification!

  • User Avatar
    0
    aaron created
    Support Team

    Understand. I just feel the "-2147482647" weird.

    So did @hikalkan on Jul 23, 2016 ;)

  • User Avatar
    0
    fguo created

    I encountered a new issue related this insert method.

    I use "var id = await _repository.InsertAndGetIdAsync(obj); ". It does return a new id, however, the database seems not really updated. My code snippet is like:

    var id = await _repository.InsertAndGetIdAsync(obj); Process proc = new Process(); proc.StartInfo.FileName = @"command"; proc.Start();

    After inserting the obj, it triggers a Process with "command". The above Process should aggregate the whole obj table, but it actually always excludes the current inserted obj. For example, the MAX(id) is not the "new id", instead, it is always "new id - 1".

    I also tried:

    var id = await _repository.InsertAndGetIdAsync(obj); UnitOfWorkManager.Current.SaveChanges();

    but it's no help.

    Does "await _repository.InsertAndGetIdAsync(obj); " immedietely updates the database? If not, when it does?

    Thanks,

  • User Avatar
    0
    aaron created
    Support Team

    It's inserted but not committed. To commit it, you can complete a new unit of work.

    using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)) {
        var id = await _repository.InsertAndGetIdAsync(obj);
        await uow.CompleteAsync();
    }
    
  • User Avatar
    0
    fguo created

    It works! Thank you!

    Just for curious, if I don't manually commit it, is it automatically committed at the method ending?

    I think I need to learn more about the UnitOfWorkManager. Where can I find the documentation? Can you provide me a link?

    Thanks again!

  • User Avatar
    0
    aaron created
    Support Team

    Yes. At the end of the method, the transaction is committed and the connection is disposed.

    You can read about IUnitOfWorkManager in the documentation on Unit Of Work.