Open Closed

Connection String from Environmental variables #1097


User avatar
0
jon.hallam@managed-desktop.com created

I want to use the system environmental variables.

I want to use them for the DBMigrator and the Web application. why I set the database parameters as system parameters for that system

Please can you tell me how I can set these?

I want to use this.

    public string GetConnectionString()
    {var combuilder = new MySqlConnector.MySqlConnectionStringBuilder();
                 combuilder.Server = Environment.GetEnvironmentVariable("DATABASE_HOSTNAME");
                
                 combuilder.UserID = Environment.GetEnvironmentVariable("DATABASE_USERNAME");
                
                 combuilder.Password = Environment.GetEnvironmentVariable("DATABASE_PASSWORD");
                 
                 combuilder.Database = Environment.GetEnvironmentVariable("DATABASE_DATABASE");
                 
                 
                 return combuilder.ToString();
        
    }
        
        then use this in both the DBMigrator, and the web app. 
        
        
  • ABP Framework version: v4.2.1
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:

5 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The framework will get connection string from AbpDbConnectionOptions by default.

    So you can custom the DefaultConnectionStringResolver or MultiTenantConnectionStringResolver service.

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Data/Volo/Abp/Data/DefaultConnectionStringResolver.cs#L29 https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/MultiTenantConnectionStringResolver.cs#L12

  • User Avatar
    0
    jon.hallam@managed-desktop.com created

    Hi this does help me, I would like some example code showing this.

    Specificly on how to get this working on the MultiTenant Connection String Resolver.

  • User Avatar
    0
    jon.hallam@managed-desktop.com created

    I get this error.

    [15:06:28 INF] Started database migrations...
    [15:06:28 INF] Migrating schema for host database...
    [15:06:30 INF] Executing host database seed...
    Unhandled exception. System.InvalidOperationException: The specified transaction is not associated with the current connection. Only transactions associated with the current connection may be used.
       at Microsoft.EntityFrameworkCore.Storage.RelationalTransaction..ctor(IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger`1 logger, Boolean transactionOwned)
       at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlRelationalTransaction..ctor(IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger`1 logger, Boolean transactionOwned)
       at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlRelationalTransactionFactory.Create(IRelationalConnection connection, DbTransaction transaction, Guid transactionId, IDiagnosticsLogger`1 logger, Boolean transactionOwned)
       at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.CreateRelationalTransaction(DbTransaction transaction, Guid transactionId, Boolean transactionOwned)
       at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.UseTransaction(DbTransaction transaction, Guid transactionId)
       at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.UseTransaction(DatabaseFacade databaseFacade, DbTransaction transaction, Guid transactionId)
       at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.UseTransaction(DatabaseFacade databaseFacade, DbTransaction transaction)
       at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.CreateDbContextWithTransaction(IUnitOfWork unitOfWork)
       at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.CreateDbContext(IUnitOfWork unitOfWork)
       at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.CreateDbContext(IUnitOfWork unitOfWork, String connectionStringName, String connectionString)
       at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.<>c__DisplayClass3_0.<GetDbContext>b__0()
       at System.Collections.Generic.AbpDictionaryExtensions.&lt;&gt;c__DisplayClass6_0`2.<GetOrAdd>b__0(TKey k)
       at System.Collections.Generic.AbpDictionaryExtensions.GetOrAdd[TKey,TValue](IDictionary`2 dictionary, TKey key, Func`2 factory)
       at System.Collections.Generic.AbpDictionaryExtensions.GetOrAdd[TKey,TValue](IDictionary`2 dictionary, TKey key, Func`1 factory)
       at Volo.Abp.Uow.UnitOfWork.GetOrAddDatabaseApi(String key, Func`1 factory)
       at Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider`1.GetDbContext()
       at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository`2.get_DbContext()
       at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository`2.get_DbSet()
       at Volo.Abp.Identity.EntityFrameworkCore.EfCoreIdentityUserRepository.FindByNormalizedUserNameAsync(String normalizedUserName, Boolean includeDetails, CancellationToken cancellationToken)
       at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)
       at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
       at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
       at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
       at Volo.Abp.Identity.IdentityDataSeeder.SeedAsync(String adminEmail, String adminPassword, Nullable`1 tenantId)
       at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo)
       at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
       at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
       at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult](IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
       at Volo.Abp.Data.DataSeeder.SeedAsync(DataSeedContext context)
       at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous(IInvocation invocation, IInvocationProceedInfo proceedInfo)
       at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapter.ProceedAsync()
       at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
       at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
       at Accountz.Data.AccountzDbMigrationService.SeedDataAsync(Tenant tenant) in /Users/jonathanhallam/Documents/GitHub/Accountz/src/Accountz.Domain/Data/AccountzDbMigrationService.cs:line 90
       at Accountz.Data.AccountzDbMigrationService.MigrateAsync() in /Users/jonathanhallam/Documents/GitHub/Accountz/src/Accountz.Domain/Data/AccountzDbMigrationService.cs:line 41
       at Accountz.DbMigrator.DbMigratorHostedService.StartAsync(CancellationToken cancellationToken) in /Users/jonathanhallam/Documents/GitHub/Accountz/src/Accountz.DbMigrator/DbMigratorHostedService.cs:line 31
       at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
       at Accountz.DbMigrator.Program.Main(String[] args) in /Users/jonathanhallam/Documents/GitHub/Accountz/src/Accountz.DbMigrator/Program.cs:line 29
       at Accountz.DbMigrator.Program.&lt;Main&gt;(String[] args)
    
    Process finished with exit code 134.
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share your ConnectionStringResolver code?

    System.InvalidOperationException: The specified transaction is not associated with the current connection. Only transactions associated with the current connection may be used.

    https://github.com/abpframework/abp/issues/8049

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 11, 2024, 11:11