Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Setting to generate context with one single constructor for pooling [FEATURE REQUEST] #72

Closed
mmarinchenko opened this issue Apr 26, 2019 · 8 comments
Labels
enhancement New feature request released Issue is resolved in a current release

Comments

@mmarinchenko
Copy link

mmarinchenko commented Apr 26, 2019

EF Core supports context poling by calling AddDbContextPool<>() instead of AddDbContext<>() inside ConfigureServices() method. But this requires context to have only one single constructor with options parameter:

InvalidOperationException: The DbContext of type 'DataContext' cannot be pooled because it does not have a single public constructor accepting a single parameter of type DbContextOptions.

To workaround this I commented the following lines in local copy of EFCoreDesigner.ttinclude file:

// Output("/// <inheritdoc />");
// Output($"public {modelRoot.EntityContainerName}() : base()");
// Output("{");
// Output("}");
// NL();

It would be great to add setting for that in Entity Context section of diagram properties :)

@mmarinchenko
Copy link
Author

To be honest I don't understand why the context in EF Core might need a default constructor without parameters.

@msawczyn
Copy link
Owner

msawczyn commented Apr 28, 2019

Now that you mention it, neither do I. I'm removing that. If anyone wants it back, it can be added to their partial. I'll put something in the docs about that - something nags at me that I put that there for a reason but, of course, didn't document it at the time so don't remember why now. (https://i.redd.it/hwqj7yx9vm211.jpg)

@msawczyn msawczyn added enhancement New feature request pending release Issue is resolved in the current codebase, will be published with the next release labels Apr 28, 2019
@msawczyn msawczyn added this to the 2.7.2 milestone Apr 28, 2019
@msawczyn msawczyn added released Issue is resolved in a current release and removed pending release Issue is resolved in the current codebase, will be published with the next release labels Apr 28, 2019
@mmarinchenko
Copy link
Author

Thanks for your support!

I tried to test this but something went wrong :(

1.2.7.2 version is not working:
A processor named 'EFModelDirectiveProcessor' could not be found for the directive named 'EFModel'. The transformation will not be run. The following Exception was thrown: System.InvalidCastException: Failed to resolve type for directive processor EFModelDirectiveProcessor. at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolveDirectiveProcessorFromRegistry(String processorName, OrchestratorPackage orchestratorPackage) at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolveDirectiveProcessor(String processorName) at Microsoft.VisualStudio.TextTemplating.Engine.ProcessCustomDirectives(ITextTemplatingEngineHost host, TemplateProcessingSession session, IEnumerable``1 directivesToBeProcessed) ClassLibrary1 D:\VSProjects\ClassLibrary1\ClassLibrary1\EFModel1.tt 17

I reverted to 1.2.7.1 and all is good...

@msawczyn
Copy link
Owner

There was an issue with the build server causing the vsix to have some issues. I've since rebuilt and verified that it runs in VS2017 and VS2019. The new build is posted in the marketplace and here on github with the same version number. Please give that a go, and my apologies for the problems.

@mmarinchenko
Copy link
Author

Yes, now it's working as expexted. Thank you!

@rlarik
Copy link

rlarik commented May 3, 2019

something nags at me that I put that there for a reason but, of course, didn't document it at the time so don't remember why now

Generating a migration without a parameterless constructor results in an error:
Unable to create an object of type 'DataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Adding the empty constructor back fixes this problem. Where in the docs have you added the information?

@mmarinchenko
Copy link
Author

@rlarik, thanks for the info!

I forgot about that because I use ASP.NET Core framework :)

In ASP.NET Core scenario both parameterless constructor and OnConfiguring() method are not needed.

@mmarinchenko
Copy link
Author

mmarinchenko commented May 6, 2019

So, maybe it would be more appropriate to add a setting regarding the ASP.NET Core framework:

  • use IdentityDbContext as base class instead of DbContext (see original point 3 in issue msawczyn/EFDesigner2022#38, other two points are not needed actually)
  • do not generate parameterless constructor for context class
  • do not generate CustomInit() and OnConfiguring() methods for context class

Default value is false.

P.S. EFCoreDesigner.ttinclude contains code related to Lazy Loading and Inheritance Strategy settings but they are not available for EF Core version of diagram properties :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature request released Issue is resolved in a current release
Projects
None yet
Development

No branches or pull requests

3 participants