0

let me start with, I am new to EF and more specifically deeper backend functionality with that being said,

Here's my scenario

I am working with an MVC web app hosted on Azure, it been in existence for quite some time, currently working with a single SQL server Database, along with a single WebSolutionDB project in my solution DB Project SS

I connect to the DB via connection strings in my app.config Please comment if more information is required, on how the app currently operates

I now have 2 DB's and I wish to have a DBContext set up for each on startup/runtime, which is Registered by the ContainerBuilder on Startup via EF's DependancyRegistrar.cs, keep in mind each table should have its own repository for CRUD operations

Here's my problem(and what I've tried)

  1. I have added the 2 connection strings in my app.config and in my Settings.txt
<connectionStrings>
    <!-- The format of the connection string is "DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY" -->
    <!-- For local execution, the value can be set either in this config file or through environment variables -->
    <add name="MasterDB" connectionString="Data Source= xxxxxxx;Initial Catalog=Db_Master;User ID=xxxx;Password= xxxxxx" />
    <add name="StagingDB" connectionString="Data Source= xxxxxxx;Initial Catalog=Db_Staging;Persist Security Info=True;User ID= xxxxxx ;Password= xxxxxxxx" />
</connectionStrings>
  1. I have created separate DBContext class files for each as follow's
public class MasterDbContext : DbContext, IDbContext
{
        #region Ctor
        public MasterDbContext()
            : base("MasterDB") 
        {
            ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 600; 
        }
}

public class StagingDbContext : DbContext, IDbContext
{
        #region Ctor
        public StagingDbContext()
            : base("StagingDB")
        {
            ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 600; 
        }
}

and the ContainerBuilder registry (DependancyRegistrar.cs) looks like this.

//data layer
            var dataSettingsManager = new DataSettingsManager();
            var dataProviderSettings = dataSettingsManager.LoadSettings();
            //builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>();
            builder.Register(x => new EFDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency();
            builder.Register(x => x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IDataProvider>().InstancePerDependency();


            if (dataProviderSettings != null && dataProviderSettings.IsValid())
            {
                var efDataProviderManager = new EFDataProviderManager(dataSettingsManager.LoadSettings());
                var dataProvider = efDataProviderManager.LoadDataProvider();
                dataProvider.InitConnectionFactory();
            }
            builder.RegisterType<StagingDBContext>()
                .AsSelf()
                .Named<IDbContext>("StagingDB")
                .InstancePerLifetimeScope();
            builder.RegisterType<MasterDBContext>()
                .AsSelf()
                .Named<IDbContext>("MasterDB")
                .InstancePerLifetimeScope();

other sources I've tried include (possible help for others), however none of them resolve my issue:

Register multiple dbcontext using autofac where builder is not available to resolve the classes after registration https://entityframework.net/knowledge-base/42139457/none-of-the-constructors-found-with--autofac-core-activators-reflection-defaultconstructorfinder--on-type EntityFramework code-first custom connection string and migrations Using entity framework on multiple databases Configure multiple database Entity Framework 6

If you need more information on the problem please comment, but otherwise, where am I going wrong?, what am I missing?

What I'm trying to achieve is to have a connection to both DB's established at runtime and have my repositories and Service classes point to the respective tables for the respective DB context.

I see no fault with the conn strings and the DBContext classes setup, I believe my issue lies with the startup configuration, right?

error I get on startup

enter image description here

Thank you for your time and response in advance.

ARITHABORT
  • 11
  • 4

0 Answers0