Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding DbContext Model diagram showing errors #655

Closed
ersaumya opened this issue Dec 12, 2020 · 21 comments
Closed

Adding DbContext Model diagram showing errors #655

ersaumya opened this issue Dec 12, 2020 · 21 comments
Labels
bug Something isn't working modelgen

Comments

@ersaumya
Copy link

Adding DbContext Model diagram in my project getting errors

Steps to reproduce

Right click project > EF core power tools > Add DbContext model diagram
An error occured.See the output window for details

Output window error:

========== Build: 0 succeeded, 0 failed, 2 up-to-date, 0 skipped ==========
System.ArgumentException: Error:
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'BlogPostContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions1[BlogPost.Infrastructure.DataContext.BlogPostContext]' while attempting to activate 'BlogPost.Infrastructure.DataContext.BlogPostContext'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13() --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at ReverseEngineer20.EfCoreModelBuilder.BuildResult(String outputPath, Boolean generateDdl) in C:\Code\EFCorePowerTools\src\GUI\efpt\EFCoreModelBuilder.cs:line 39
at ReverseEngineer20.Program.Main(String[] args) in C:\Code\EFCorePowerTools\src\GUI\efpt\Program.cs:line 42

Parameter name: processResult
at EFCorePowerTools.Handlers.ModelAnalyzerHandler.d__2.MoveNext() in C:\projects\efcorepowertools\src\GUI\EFCorePowerTools\Handlers\ModelAnalyzerHandler.cs:line 78

Further technical details

EF Core Power Tools version: 2.5.277

Database engine: SQL Server

Visual Studio version:Microsoft Visual Studio Community 2019 Version 16.8.0

csproj file:
TargetFramework:net5.0
Microsoft.EntityFrameworkCore.Design Version="5.0.1"
Microsoft.EntityFrameworkCore.SqlServer Version="5.0.1"
Microsoft.EntityFrameworkCore.Tools Version="5.0.1"

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 12, 2020

Did you check the link in the error message?

I also have some tips in the wiki.

@ersaumya
Copy link
Author

Yes i have checked the link

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 12, 2020

If it is still not working for you, I need a repro project.

@ersaumya
Copy link
Author

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 12, 2020

Modifying the first part of your DbContext like this fixes the issue:

        public BlogPostContext()
        { }

        public BlogPostContext(DbContextOptions<BlogPostContext> options) : base(options)
        { }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Data Source=.");
            }
        }

@ErikEJ ErikEJ closed this as completed Dec 12, 2020
@ErikEJ
Copy link
Owner

ErikEJ commented Dec 12, 2020

@bricelam If if swap the order of the constructors, the DbContext discovery fails. Is that by design (so to speak) ?

@ersaumya
Copy link
Author

ersaumya commented Dec 13, 2020

@ErikEJ Thanks for quick response its working now. Seems the order of constructors to be followed?

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 13, 2020

Yes, that is what I am curious about.

@bricelam
Copy link
Contributor

bricelam commented Jan 5, 2021

dotnet/runtime#45119

@ErikEJ ErikEJ reopened this Jan 6, 2021
@ErikEJ ErikEJ added the bug Something isn't working label Jan 6, 2021
@dfjackson
Copy link

I'm also experiencing a similar issue. I was getting an exception similar to above, but I added a constructor that does not take any arguments to the DbContext class, and this error is resolved. After fixing that, at the bottom of the screen it says "An error occurred. See the output window for details." but there are no error messages in the output window and it does not generate the model. Please let me know if you need more info.

@ErikEJ
Copy link
Owner

ErikEJ commented Sep 2, 2021

@dfjackson what version of the extension?

A repro project would help.

@dfjackson
Copy link

I'm using version 2.5.692.0 of the extension. What is the easiest way to send you a repro project?

@ErikEJ
Copy link
Owner

ErikEJ commented Sep 3, 2021

@dfjackson you can email me at ejlskov at hotmail dot com

@ErikEJ
Copy link
Owner

ErikEJ commented Sep 3, 2021

Have you looked at the "Extensions" window, btw?

@dfjackson
Copy link

I had not noticed the Extensions option in the Output window. Here's the exception I'm getting now in that window (which looks similar to the one above):

System.ArgumentException: Error:
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'MyAppContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions1[Stuff.Model.EfCore.MyApp.MyAppContext]' while attempting to activate 'Stuff.Model.EfCore.MyApp.MyAppContext'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13() --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Modelling.EfCoreModelBuilder.BuildResult(String outputPath, String startupOutputPath, Boolean generateDdl) in C:\Code\EFCorePowerTools\src\GUI\efpt30.core\EFCoreModelBuilder.cs:line 39
at Modelling.Program.Main(String[] args) in C:\Code\EFCorePowerTools\src\GUI\efpt30.core\Program.cs:line 57

Parameter name: processResult
at async Task EFCorePowerTools.Handlers.ModelAnalyzerHandler.GenerateAsync(string outputPath, Project project, GenerationType generationType)
System.ArgumentException: Error:
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'MyAppContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions1[Stuff.Model.EfCore.MyApp.MyAppContext]' while attempting to activate 'Stuff.Model.EfCore.MyApp.MyAppContext'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13() --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Modelling.EfCoreModelBuilder.BuildResult(String outputPath, String startupOutputPath, Boolean generateDdl) in C:\Code\EFCorePowerTools\src\GUI\efpt30.core\EFCoreModelBuilder.cs:line 39
at Modelling.Program.Main(String[] args) in C:\Code\EFCorePowerTools\src\GUI\efpt30.core\Program.cs:line 57

Parameter name: processResult
at async Task EFCorePowerTools.Handlers.ModelAnalyzerHandler.GenerateAsync(string outputPath, Project project, GenerationType generationType)

Now I'm using extension version 2.5.744.0. I was able to upload a zip file of a solution that reproduces the issue:

repro.zip

@ErikEJ
Copy link
Owner

ErikEJ commented Sep 3, 2021

And the constructor ordering??

@ErikEJ
Copy link
Owner

ErikEJ commented Sep 4, 2021

I will update the status bar message!

@ErikEJ
Copy link
Owner

ErikEJ commented Sep 4, 2021

@dfjackson I tried you repro, but your DbContext is crippled, and does not build.

You are not using AddDbContext in your startup, I assume that is why design discovery fails.

I think you can make the class library your startup project, and make it work if you add this to your DbContext:

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Data Source=.");
            }
        }

@dfjackson
Copy link

Sorry the repro was not completely working, but I got it working after adding the OnConfiguring method. The same thing fixed it in my real code too. Also I think updating the status bar message will help. Thanks!

@ErikEJ
Copy link
Owner

ErikEJ commented Sep 7, 2021

@dfjackson Thanks for the heads up and the good news!

@ErikEJ
Copy link
Owner

ErikEJ commented Nov 22, 2021

@ErikEJ ErikEJ closed this as completed Nov 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working modelgen
Projects
None yet
Development

No branches or pull requests

4 participants