Page 1 of 1

DbContext Constructor

Posted: Sun 11 Dec 2016 18:23
by guinness
When creating EF Core Model from database would be possible to add the following additional constructor to the DbContext when using Generate Code?

Code: Select all

public myContext(DbContextOptions<myContext> options) : base(options) { }
This is needed for when Dependency Injection is being used:

Code: Select all

services.AddDbContext<myContext>(options => options.UseMySql(Configuration.GetConnectionString("myConnection")));
I could also add it manually since it's a partial class, but it seems this would be a common need.

Re: DbContext Constructor

Posted: Thu 15 Dec 2016 13:26
by Shalex
Thank you for your suggestion. We will notify you when the new constructor is added.

Re: DbContext Constructor

Posted: Thu 22 Dec 2016 18:45
by Shalex
The new (6.0.157) build of Entity Developer includes an updated predefined EF Core template (constructor added).

Re: DbContext Constructor

Posted: Wed 07 Jun 2017 19:52
by guinness
@Shalex - thanks, I confirm this is working in 6.1.265 version.

Is there anyway to remove the OnConfiguring method from the context? I have no need for it and would rather not have the connection string (including credentials) stored in our source control system.

Re: DbContext Constructor

Posted: Fri 09 Jun 2017 10:10
by Shalex
Please navigate to Model Settings, select "Use the following connection string from App.config" and clear selection of "Rewrite connection string during regeneration". After connection string is generated in app.config first time, delete it. Now no connection string will be created when saving the model and OnConfiguring will look like:

Code: Select all

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseOracle(GetConnectionString("MyModelConnectionString"));
            CustomizeConfiguration(ref optionsBuilder);
            base.OnConfiguring(optionsBuilder);
        }
Otherwise, please modify a predefined template to remove OnConfiguring from generation. In this case you should call optionsBuilder.UseOracle manually.

Re: DbContext Constructor

Posted: Fri 09 Jun 2017 19:12
by guinness
Thanks, I took the former option. The latter seemed less attractive as I would have to update template in each subsequent application upgrade.

Note: You must also uncheck "Allow saving password" option in Connection properties dialog, otherwise the password will be stored in the .edps file which may be added to source control.

Re: DbContext Constructor

Posted: Thu 15 Jun 2017 23:57
by guinness
Please note that the following code in the EF Core template will not compile if using netstandard1.x framework:

Code: Select all

private static string GetConnectionString(string connectionStringName)
{
    System.Configuration.ConnectionStringSettings connectionStringSettings = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName];
    if (connectionStringSettings == null)
        throw new InvalidOperationException("Connection string \"" + connectionStringName +"\" could not be found in the configuration file.");
    return connectionStringSettings.ConnectionString;
}
The reason is that System.Configuration is only available in the full framework. The new way would be to add

Code: Select all

services.AddSingleton<IConfiguration>(Configuration)
into Startup.cs and then inject IConfiguration into the DbContext then using

Code: Select all

config.GetConnectionString(connectionStringName)
in the GetConnectionString method.

Re: DbContext Constructor

Posted: Fri 16 Jun 2017 18:59
by Shalex
Thank you for your report. We will notify you when the issue is fixed.

Re: DbContext Constructor

Posted: Wed 28 Jun 2017 11:28
by Shalex
The bug with generating .NET Core code by EF Core template for the "Use the following connection string from App.config" option in Model Settings is fixed: viewtopic.php?f=32&t=35583.

JIC:
1. The package "Microsoft.Extensions.Configuration.Json" should be installed manually.
2. If necessary, turn on the "Rewrite connection string" option in Model Settings.