Entity Framework Core dbcontextoptionsbuilder does not contain a definition for usesqlserver – Solved
Entity Framework Core dbcontextoptionsbuilder does not contain a definition for usesqlserver – Solved

Introduction

In this post, I will show how to solve the error that shows "Entity Framework Core dbcontextoptionsbuilder does not contain a definition for UseSqlServer" with ASP.Net Core 3 and 3.1 framework.

Overview

We wanted to connect our ASP.NET Core application with MS SQL. To do this, we configure a connection in the startup class.

 // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContextPool(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DbContext")));

            services.AddControllers();
        }

However, when I did the same setup with ASP.NET Core 3.1, I ran into issue. Compiler complained that "Entity Framework Core dbcontextoptionsbuilder does not contain a definition for UseSqlServer".

Entity Framework Core dbcontextoptionsbuilder does not contain a definition for usesqlserver

Solution

After digging into the issue, I figured out there was a small change done from Microsoft and to make this work you need to just add a package called "Microsoft.EntityFrameworkCore.SqlServer".

Install NuGet Package Microsoft.EntityFrameworkCore.SqlServer

    
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />

Or

PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer

Just remove or comment the lines that are showing as error and install the package. That's it!

dbcontextoptionsbuilder does not contain a definition for usesqlserver solved

Also see how to Implement create, read, update, and delete functionalities using Entity Framework Core

Conclusion

Thanks for reading this piece. As you can see, the method is not moved to a different namespace from ASP.NET Core 3.0 version and installing this extra package will solve the problem. Let me know in the comments if you have questions.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights