entity-framework-core

Unknown connection string name in Entity Framework Core DbContext


I posted a couple of days ago about this.

Entity Framework Core code-first connection string for multiple repositories

The responses got me to refactor some things, but I still have an couple of unanswered questions, so let me clarify the problem statement.

As part of my own framework I have a separate, stand-alone C# / .NET Core class library that handles application security for whatever app I want to user it in. It can be used in any .NET Core solution.

For example, if I create an app called Acme.Widgets, and I need to handle users, roles, right, etc., I can bring in my Framework.AppSecurity project.

While the security tables will actually be contained in the Acme.Widgets database, the DbContext for them exists in the AppSecurity project.

So, in the AppSecurity project, I created a DbContext class. Please see my comment near the bottom:

namespace Framework.AppSecurity
{
    internal class AppSecurityDataContext : DbContext
    {
        private IConfigurationRoot _configuration;

        private DbContextOptionsBuilder<AppSecurityDataContext> _optionsBuilder;

        public virtual DbSet<UserEntity> Users { get; set; } = null!;

        public AppSecurityDataContext()
        {
        }

        public AppSecurityDataContext(DbContextOptions options)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);

            if (!optionsBuilder.IsConfigured)
            {
                var builder = new ConfigurationBuilder()
                                    .SetBasePath(Directory.GetCurrentDirectory())
                                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

                _configuration = builder.Build();

                _optionsBuilder = new DbContextOptionsBuilder<AppSecurityDataContext>();

                // How do I get the connection string name from whatever app is use this class?
                var connString = _configuration.GetConnectionString("connStringName");  
                _optionsBuilder.UseSqlServer(connString);
            }
        }
    }
}

First, how does this class get the connection string from Acme.Widgets? Assuming it's in the Acme.Widgets appsettings.json file, how would the GetConnectionString method know the name of the connection string? Remember, this class is independent of any solution.

Second, if I make changes to the AppSecurity tables and need to run a migration in the AppSecurity project, how does the migration get the connection string?

I'm using Entity Framework Core 6.0.36.

Thank you


Solution

  • When the DbContext is located in a Class Library assembly, the default location it will look for a configuration file (appsettings.json, web.config, etc.) will be from whichever executable assembly references the class library, in your case the Acme.Widgets. The default name of the connection string will be the name of the DbContext. ("AppSecurityDataContext")

    Generally though it is a good idea to have your application's IoC Container or initialization code initialize a DbContext using the DbContextOptions constructor rather than using OnConfiguring as you can configure the providers and have more conditional control over how the DbContext is initialized.

    Note that EF Core 6 and .Net 6 is already out of LTS. The support cycle for MS software has changed to essentially require .Net Core projects to be actively maintained and upgraded as the framework is upgraded.