Using sql server with entity framework core

In this video we will discuss how to configure and use SQL Server with entity framework core.

Text version of the video


ASP.NET Core Text Articles & Slides

ASP.NET Core Tutorial

Angular, JavaScript, jQuery, Dot Net & SQL Playlists

When using Entity Framework Core, one of the important things that we need to configure is the database provider that we plan to use. Entity Framework Core supports a wide variety of databases including non-relational databases. The following MSDN link has the list of all supported databases.

public class Startup
private IConfiguration _config;

public Startup(IConfiguration config)
_config = config;

public void ConfigureServices(IServiceCollection services)
options =] options.UseSqlServer(_config.GetConnectionString(“EmployeeDBConnection”)));

services.AddTransient[IEmployeeRepository, MockEmployeeRepository]();

// Rest of the code

We want to configure and use Microsoft SQL Server with entity framework core.

We usually specify this configuration in ConfigureServices() method in Startup.cs file.

We can use either AddDbContext() or AddDbContextPool() method to register our application specific DbContext class with the ASP.NET Core dependency injection system.

The difference between AddDbContext() and AddDbContextPool() methods is, AddDbContextPool() method provides DbContext pooling.

With DbContext pooling, an instance from the DbContext pool is provided if available, rather than creating a new instance.

DbContext pooling is conceptually similar to how connection pooling works in ADO.NET.

From a performance standpoint AddDbContextPool() method is better over AddDbContext() method.

AddDbContextPool() method is introduced in ASP.NET Core 2.0. So if you are using ASP.NET Core 2.0 or later use AddDbContextPool() method over AddDbContext() method.

UseSqlServer() extension method is used to configure our application specific DbContext class to use Microsoft SQL Server as the database.

To connect to a database, we need the database connection string which is provided as a parameter to UseSqlServer() extension method

options =] options.UseSqlServer(_config.GetConnectionString(“EmployeeDBConnection”)));

Instead of hard-coding the connection string in application code, we store it appsettings.json configuration file.

“ConnectionStrings”: {
“EmployeeDBConnection”: “server=(localdb)\MSSQLLocalDB;database=EmployeeDB;Trusted_Connection=true”

In classic we store application configuration in web.config file which is in XML format. In core, there are different configuration sources. One configuration source is appsettings.json file and it is in JSON format.

To read connection string from appsettings.json file we use IConfiguration service GetConnectionString() method.

We are usin SQL Server localdb which is automatically installed along with Visual Studio. If you want to use a full blown SQL Server instead of localdb, simply change the connection string in appsettings.json configuration file to point to your instance of SQL Server.


What is the difference between the following in a database connection string
Integrated Security=SSPI;
Integrated Security=true;

All the above 3 settings specify the samething, use Integrated Windows Authentication to connect to SQL Server instead of using SQL Server authentication.

At the moment our application is still using MockEmployeeRepository which is an in-memory collection of employees. In our next video we will implement SQLRepository which stores and retrieves employees from sql server localdb that we have just configured.


Xem thêm bài viết khác:

38 thoughts on “Using sql server with entity framework core

  1. how to configure is I need for example: an IDepartmentRepository, etc because in this project we just use one model(Employee), and Department is an enum so, how can i do if i need more models?? anyone knows?

  2. Sir, could you please advise how to connect Sybase database using entity framework in .net core….

  3. sir,
    how to create table if table not exists to existing database in ef core ?

    we don't use migration because our existing databases are with clients devices and want to create additional tables to their databases after updates for feature extensions…

    there was an option like Context.DbEntity.Create(); in ef6 but not getting in ef core to do so…

    I used the below but it also not creating tables which doesn't exist…



    var databaseCreator = (Database.GetService<IDatabaseCreator>() as RelationalDatabaseCreator);



    catch (Exception ex)




  4. Hi, in the services.AddMVC i have a few errors, namely that the migrations to ASP.NETCore 3.0 show the error "Endpoint Routing does not support 'IApplicationBuilder.UseMvc(…)'. To use 'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices(…).'

    Ive tried enabling endpointrouting to false, but that prevents use of entity framework and doesnt detect my context at all, any advice please?

  5. For to make "UseSqlServer" work with ASP NET Core 3.1 in Visual Studio 2019 v16.4.3, i had to install Microsoft.EntityFrameworkCore.SqlServer 3.1.1 NuGet package.

  6. Notice that you are a great teacher. The point that i am trying to make is that i like the way you explain. That's all in this comment, thank you for reading.

  7. FYI, update for .NET Core 3.0
    AddDbContext and AddDbContextPool is no longer available directly because EF is no longer part of the meta-package, so you need to add the reference to EF package explicitly
    check this doc

  8. Great videos but getting suck here. I am getting error "Error CS1593 Delegate 'Action<IServiceProvider, DbContextOptionsBuilder>' does not take 1 arguments" on the


    options => options.UseSqlServer(_config.GetConnectionString("EmployeeDBConnection")));

    any ideas?

  9. Do you know how to scaffold views and sprocedures from SQL Server to our project by using ASP.NET Core 2.2?

  10. Interesting to see multiple calls to Data Base in Index View if we use the model several times (foreach) in the page ?!
    I'm still not convinced in using Entity Framework…

  11. If someone is doing the project step by step. Can you please send me the code for the project till here? I would like to catch up from here. Can you send it on that would be really helpful

  12. Sir can you make a vide on uploading and downloading images and/or videos in angular 7 and ASP.NET CORE. It would be very helpful.

  13. Hi…Regarding the above video, when i try to pass the created context class 'AppDbContext' to the 'AddDbContextPool', it gives me a error like 'Static types cannot be used as type arguments'. Could you please help me in resolving this.

  14. first thank you so much for your videos, my request is what do you say dapper vs entity framework. I'm waiting for your suggestions/answer

  15. Thank you Venkat, for teaching us entire Asp.Net Core series diligently. You explain every concept quite clearly, so we can understand them very well. very kind of you putting lot of efforts in bringing these teaching series together. Thanking you from the bottom of my heart.

  16. Thank you so much for your videos,please upload videos for exporting and importing excel data to table and gridview.

  17. thank you alot. Please if you can to provide a course to boilerplate framework it's more powerfull framework and depends on design driven development

  18. Great job venkat, can you please make parts video starting from insert, delete, update, retrieving data from simple case until using bootstrap and EF? thank you

  19. The Dapper ORM is a million times easier to setup and work with. you can create, update, delete, read with 2 lines of code.

Leave a Reply

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