Base solution for your next web application
Open Closed

Asp net core - multiple contexts - default connection string #4354


User avatar
0
janhu created

Hi,

I Added CompanyDbContext to my project new to OriginalDbContex created by the template. I also regisetered it at OriginalEntityFrameworkCoreModule. It looks like this:

Configuration.Modules.AbpEfCore().AddDbContext<CompanyDbContext>(options =>
                {
                    if (options.ExistingConnection != null)
                    {
                        CompanyDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                    }
                    else
                    {
                        
                        CompanyDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                    }
                });

The problem is that the

options

parameter always contains default connecion string... I tried to register it at startup.cs which did not work either.

services.AddDbContext<CompanyDbContext>(options=>
            
            {
                var connectionString=_appConfiguration["ConnectionStrings:CS_Test"];
                options.UseSqlServer(connectionString);
            });

Can you help me to obtain the CS_Test parameter please?

I also implemented IDesignTimeDbContextFactory<CompanyDbContext> the same as the OriginalFbContext has. But the comment above it says

/* This class is needed to run "dotnet ef ..." commands from command line on development. Not used anywhere else */

Where is the problem then?

Thank you.


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

    Thank you. I created my own ConnectionStringResolver. Unfortunaltely the configuration provided in ConfigurationManager did not contain the connection string from appsettings.json so I had to hard code id.

    So be it... issue is resolved even though I am very disappointed or I did not get it correctly.

  • User Avatar
    0
    alper created
    Support Team

    hi, don't get dissappointed :) how about this one?

    var appsettingsjson = JObject.Parse(File.ReadAllText("appsettings.json"));
    var connectionStrings = (JObject)appsettingsjson["ConnectionStrings"];
    var connectionString = connectionStrings.Property("Default").Value.ToString();