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

"Cannot insert explicit value" error in custom EntityHistoryStore #6141


User avatar
0
BobIngham created

dotnet-core, angular, 5.4.1 - One for Aaron. I think. Hi Aaron, If you remember I implemented my own EntityHistoryStore inheriting from IEntityHistoryStore to write entity change history to Mongodb. This works well but the other day Azure reconfigured my outgoing Ip's for some reason and the new IP's were not configured in my Mongodb Atlas whitelist. I fixed this issue but realised that I would need a fallback should, for any reason, my Mongodb data store become unavailable. I implemented the following fallback code in my implementation of EntityHistoryStore:

try
{
    //standard timeout for Mpongodb is 30 seconds, here we need to the system to return to the client as quickly as possible
    using (var timeoutCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(2000)))
    {
        await collection.InsertOneAsync(document);
    }
}
catch (Exception ex)
{
    logger.Error("Mongodb: " + ex.GetType(), ex);
    //fallback and save to SQL Server until Mongodb is back up and running
    await _fallbackEntityHistoryStore.SaveAsync(changeSet);
}

If I cannot write to Mongodb the system will fallback to my FallbackEntityHistoryStore which is simple in implementation:

public class FallbackEntityHistoryStore : IFallbackEntityHistoryStore, ITransientDependency
{
    private readonly IRepository<EntityChangeSet, long> _changeSetRepository;
    private readonly IUnitOfWorkManager _unitOfWorkManager;

    public FallbackEntityHistoryStore(
        IRepository<EntityChangeSet, long> changeSetRepository,
        IUnitOfWorkManager unitOfWorkManager
        )
    {
        _changeSetRepository = changeSetRepository;
        _unitOfWorkManager = unitOfWorkManager;
    }

    public async Task SaveAsync(EntityChangeSet changeSet)
    {
        using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
        {
            await _changeSetRepository.InsertAsync(changeSet);
            await uow.CompleteAsync();
        }
    }
}

This works well on the first call but then fails on subsequent calls with the message:

Cannot insert explicit value for identity column in table '[dbo].[AbpEntityChangeSets]' when IDENTITY_INSERT is set to OFF.

I can reset the identity_insert value in the database directly with SET IDENTITY_INSERT dbo.AbpEntityChangeSets ON and the next iteration will work again but it sets the identity_insert value of the table back to OFF.

Is there some piece of magic which you use in your solution that I can implement in my solution? I have searched your source code on github but I can see nothing you are doing different. Googling the issue suggests I should change data annotations on the models but I don't remember changing any of your models, I built new ones to support Mongdb Bson documents.

Any pointers what to do next? Cheers, Bob


10 Answer(s)
  • User Avatar
    0
    BobIngham created

    any ideas, @aaron?

  • User Avatar
    0
    aaron created
    Support Team

    changeSet.Id should not be set. Can you try resetting it to null?

  • User Avatar
    0
    BobIngham created

    Thanks, Aaron, sorry I've not got back to you. I will take a closer look at this over the weekend.

  • User Avatar
    0
    BobIngham created

    Hi Aaron, changeSet.Id = null;

    Cannot convert to 'long' because it is a non-nullable value type

  • User Avatar
    0
    aaron created
    Support Team

    What is the value of changeSet.Id at that point?

  • User Avatar
    0
    BobIngham created

    Hi Aaron, thanks for helping. The value of changeSet.id is 0 at that point.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bobingham

    Have you solved this problem ?

  • User Avatar
    0
    BobIngham created

    Nope, it's on a backburner but I do need to get back to it because I have no fallback poisition should Mongo Atlas not be available. The last time Azure reconfigured my app, changed IP's and they were not in my Mongo Atlas whitelist so I had a point of failure.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bobingham

    Please reopen if this is still a problem for you.

  • User Avatar
    0
    BobIngham created

    Ok. It is still a problem and I have it on my issues list but there are more pressing issues. Re-opening this means having to get to know the entity history module all over again plus the changes you implemented at a later date. Thanks for the heads up.