How do I set the OnConfiguring method in my DbContext without any hardcoded connection string?

0

I'm using multiple examples on how to set up my MySql service but in almost every one (including the Microsoft), they are using a hard coded connection string.

This is my startup:

    services.AddDbContext<DbContext>(options =>
    {
        options.UseMySql(configuration.GetConnectionString("DefaultConnection"),
        mysqlOptions =>
        {
            mysqlOptions.ServerVersion(new ServerVersion(new Version(8, 0, 18)));
        });
    });

And whenever I try to Update-Database I see an error telling me that there's no password setted

MySql.Data.MySqlClient.MySqlException (0x80004005): Access denied for user 'root'@'localhost' (using password: YES).

After looking around a bit I found an overrideable method called OnConfiguring, but I want to set the connection string in a dynamic way, cause If I change my environment, I want that string to change too.

This is where I found almost every example with hardcoded strings (ex. https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core.html)

c#
asp.net-core
entity-framework-core
asked on Stack Overflow Dec 30, 2019 by (unknown user) • edited Dec 30, 2019 by Jawad

4 Answers

0

in configuration file, you should have a ConnectionStrings that points to your database with other options.

"ConnectionStrings": {
   "DefaultConnection": "server=127.0.0.1;uid=root;pwd=12345;database=test"
},

When you use configuration.GetConnectionString, you are looking up a value from the configuration section.

answered on Stack Overflow Dec 30, 2019 by Jawad
0

You need to init configuration if not configured.

This can be done by overriding the OnConfiguring method in DbContext.

This way you can have global application configuration that will be pulled when application is ran. And use local configuration when doing Db migration.

Try this

public class YourContext: DbContext
{

    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        if (options.IsConfigured == false)
            options.UseMysql({YOUR_CONNECTION_STRING});
    }
}
answered on Stack Overflow Dec 30, 2019 by Parimal Raj
0

Try this at startup.cs

public class Startup
{
    private readonly IConfiguration _config;

    public Startup(IConfiguration config) 
    {
        _config = config;
    }

    // This method gets called by the runtime. Use this method to add services to the 
    container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<MainContext>(options => 
           options.UseOracle(_config["CONNECTION_STRING"]));
    }
 }

And do that at your Context ctor

public MainContext(DbContextOptions<MainContext> options) : base(options) { }
answered on Stack Overflow Dec 30, 2019 by everLucas
0

Based on the comment, I think you want to separate the EF Core related classes to a different assembly.

In the UseMySql there is an option to configure MigrationsAssembly. You can get more details here

Here is pseudo-code for SQL Server.

services.AddDbContext<EFCoreDemoContext>(options =>
{
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
            assembly => assembly.MigrationsAssembly(typeof(EFCoreDemoContext).Assembly.FullName));
});
answered on Stack Overflow Dec 31, 2019 by Anuraj

User contributions licensed under CC BY-SA 3.0