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.
public class Contact { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public string City { get; set; } public string State { get; set; } public string PostalCode { get; set; } public string Phone { get; set; } public string Email { get; set; } }
And the DbContext looked like this.
public class ContactsDbContext : DbContext { public DbSet<Contact> Contacts { get; set; } }
Finally the ConfigureServices function of Startup.
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ContactsDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); services.AddControllers(); }
The Error
At this point, I ran the following command from the command prompt to add a migration.
dotnet ef migrations add Initial
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.
public ContactsDbContext(DbContextOptions<ContactsDbContext> options) : base(options) { }
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.
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite("DataSource=app.db");
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.
Also published on Medium.
Hello,
Interesting information. But, what about having the DbContext in a separate project? In such scenario the above solution doesn’t work. I think that the solution of instantiating the IConfiguration object within the OnConfiguring method in order to get the connection string is just ugly!
Regards,
Jorge.
thank you man
Works for me, thank you
Hi,
Could you suggest a solution for a scenario where even having declared the dbcontext as above, the error doesn’t seem to go away?
In my case it was a typo in the appsettings.json file (invalid json).
Solution that worked,
use connection from DI
just inject the database context in to the class where you are calling it, for instance if using a controller then it will work.
public class WorkflowController : Controller
{
// GET: WorkflowController
private readonly ApplicationDbContext context;
Hey, after creating MyDbContext.cs file, I’ve created Startup.cs file to have ConfigureServices method (in the line : options.UseSqlite(Configuration.GetConnectionString(“DefaultConnection”))); ) I get an error that states “CS0117 Configuration does not contain a definition for GetConnectionString()”
Please help me resolve this error.
For those who still have problems, my case is that I have two solutions, one MVC and another Class Library (here I have the context DB).
Something else through tests I have seen that if you are on windows, it is important to mark MVC as the main solution and uncheck the other one and inject the context in Program.cs. Ah when running it works.
I had the same problem on Linux with the same project, for which to run the migrations I had to do it like this:
dotnet dotnet-ef migrations add Init -p ./ProjectWithDBContext.csproj -s ../WebApp/WebAppMVC.csproj
if you have 2 constructors in the DBContext file remove the parameter less, then it will start working.