DbContext Constructor

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
guinness
Posts: 25
Joined: Tue 22 Nov 2016 03:19

DbContext Constructor

Post by guinness » Sun 11 Dec 2016 18:23

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.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: DbContext Constructor

Post by Shalex » Thu 15 Dec 2016 13:26

Thank you for your suggestion. We will notify you when the new constructor is added.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: DbContext Constructor

Post by Shalex » Thu 22 Dec 2016 18:45

The new (6.0.157) build of Entity Developer includes an updated predefined EF Core template (constructor added).

guinness
Posts: 25
Joined: Tue 22 Nov 2016 03:19

Re: DbContext Constructor

Post by guinness » Wed 07 Jun 2017 19:52

@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.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: DbContext Constructor

Post by Shalex » Fri 09 Jun 2017 10:10

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.

guinness
Posts: 25
Joined: Tue 22 Nov 2016 03:19

Re: DbContext Constructor

Post by guinness » Fri 09 Jun 2017 19:12

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.

guinness
Posts: 25
Joined: Tue 22 Nov 2016 03:19

Re: DbContext Constructor

Post by guinness » Thu 15 Jun 2017 23:57

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.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: DbContext Constructor

Post by Shalex » Fri 16 Jun 2017 18:59

Thank you for your report. We will notify you when the issue is fixed.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: DbContext Constructor

Post by Shalex » Wed 28 Jun 2017 11:28

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.

Post Reply