Using sql server with entity framework core

1 Просмотры
In this video we will discuss how to configure and use SQL Server with entity framework core.

Text version of the video

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.


Core Text Articles & Slides

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 =] (("EmployeeDBConnection")));

[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 file.

We can use either AddDbContext() or AddDbContextPool() method to register our application specific DbContext class with the 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

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

AddDbContextPool() method is introduced in Core So if you are using Core 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 =] (("EmployeeDBConnection")));

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

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

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

To read connection string from 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 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.
Трендовое видео
Комментариев нет.