Open Closed

How to disable TransactionBehavior MongoDb when using both mysql and mongodb #502


User avatar
0
hungvt created
  • ABP Framework version: v3.2.0 I used UnitOfWorkTransactionBehavior.Disabled for mongoDb project, then UnitOfWorkTransactionBehavior of mysql as disable. How to disable only Mongo project. Configure<AbpUnitOfWorkDefaultOptions>(options => { options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; }); Thank!

3 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    There is no good way to disable yet, You can replace the UnitOfWorkMongoDbContextProvider class and remove CreateDbContextWithTransaction method

  • User Avatar
    0
    hungvt created

    Thank, How to replace the UnitOfWorkMongoDbContextProvider class?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Try

    [Dependency(ReplaceServices = true)]
        public class MyUnitOfWorkMongoDbContextProvider<TMongoDbContext> : UnitOfWorkMongoDbContextProvider<TMongoDbContext> where TMongoDbContext : IAbpMongoDbContext
    {
        public MyUnitOfWorkMongoDbContextProvider(IUnitOfWorkManager unitOfWorkManager, IConnectionStringResolver connectionStringResolver) : base(unitOfWorkManager, connectionStringResolver)
        {
        }
    
        public new TMongoDbContext CreateDbContextWithTransaction(
            IUnitOfWork unitOfWork,
            MongoUrl url,
            MongoClient client,
            IMongoDatabase database)
        {
            var dbContext = unitOfWork.ServiceProvider.GetRequiredService<TMongoDbContext>();
            dbContext.ToAbpMongoDbContext().InitializeDatabase(database, null);
    
            return dbContext;
        }
    }
    
Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35