Entity Framework Core: No database provider has been configured for this DbContext

When writing ASP. NET Core 3: Add Entity Framework Core to Existing Project I got to the point where I was going to add my initial Entity Framework Core migration when I got a huge error message with the last bit being the following in red.

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext. OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

State of the Project

The project I was working on was an API that had a single model defined as the following.

And the DbContext looked like this.

Finally the ConfigureServices function of Startup.

The Error

At this point, I ran the following command from the command prompt to add a migration.

Which results in the following error.

Stay Calm, Read, and Fix

Don’t make the same mistake I did and runoff and double-check everything in your application. The yellow and red sections of the exception message tell you what the fix should be.

In the case of the application in question, I wanted to use the connection string setup in Startup. ConfigureServices. To do that, as the error states if you bother to read it, the DbContext needs a constructor added that takes a DbContextOptions and passes that value to the base class’ constructor like the following.

Alternatively, if you aren’t to the point in your application that you want to get your database information from configuration you can override OnConfiguring in your DbContext and set your connection string there like the following.

I don’t recommend the second option, but since it is valid I feel like it needs to be included.

Wrapping Up

For me, this served as a good reminder to slow down and actually read errors even if they are a wall of text. Hopefully making myself write this post will help this lesson stick.

TheCodeBuzz

Angular – NullInjectorError No provider for HttpClient

Issue Description

Angular runtime gives below error,

In Chrome or other browser,

Angular: NullInjectorError No provider for HttpClient

Resolution

The issue is more due to not registering the required services i. e HttpClientModule in the root module ie. NgModule.

As per Angular Design and Architecture every service (internal or external service) required to be registered with root NgModule as required.

This issue could exist in the Application and in Unit Test project also. Please follow the below steps to resolve the issue.

Please register HttpClientModule in the root NgModule. File location app. module. ts

Add import statement as below,

Also please update @NgModule decorator within import section as below in the same file,

Example:

Angular: NullInjectorError No provider for HttpClient

Configure for Test project

While writing the Unit test for HttpClient dependent services you may find it is very useful to use HttpClientTestingModule.

This module is very helpful testing especially Data services that make HTTP calls to the Servers. Please import the HttpClientTestingModule and the mocking controller, HttpTestingController, along with the other symbols your tests require.

Please add below import statement,

Then please add the HttpClientTestingModule to the TestBed.

Example:

Angular: NullInjectorError No provider for HttpClient

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

References:

Thank you for reading. Please let me know your questions, thoughts or feedback below in the comments section. I appreciate your feedback and encouragement.

Please bookmark this page and share this article with your friends and Subscribe to the blog to get a notification on freshly published best practices of software development.

Источники:

https://elanderson. net/2019/11/entity-framework-core-no-database-provider-has-been-configured-for-this-dbcontext/

https://www. thecodebuzz. com/angular-null-injector-error-no-provider-for-httpclient/

Понравилась статья? Поделиться с друзьями:
Добавить комментарий

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: