DbContext in entity framework core

preview_player
Показать описание
In this video we will discuss the significance of the DbContext class in 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.

Slides

ASP.NET Core Text Articles & Slides

ASP.NET Core Tutorial

Angular, JavaScript, jQuery, Dot Net & SQL Playlists

One of the very important classes in Entity Framework Core is the DbContext class. This is the class that we use in our application code to interact with the underlying database. It is this class that manages the database connection and is used to retrieve and save data in the database.

To use the DbContext class in our application
We create a class that derives from the DbContext class.
DbContext class is in Microsoft.EntityFrameworkCore namespace.

public class AppDbContext : DbContext
{ }

For the DbContext class to be able to do any useful work, it needs an instance of the DbContextOptions class.
The DbContextOptions instance carries configuration information such as the connection string, database provider to use etc.
To pass the DbContextOptions instance we use the constructor as shown in the example below.
We will discuss more about the DbContextOptions class in our next video when we discuss database connection string in ASP.NET Core.

public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions[AppDbContext] options)
: base(options)
{
}
}

The DbContext class includes a DbSet[TEntity] property for each entity in the model.
At the moment in our application we have, only one entity class - Employee.
So in our AppDbContext class we only have one DbSet[Employee] property.
We will use this DbSet property Employees to query and save instances of the Employee class.
The LINQ queries against the DbSet[TEntity] will be translated into queries against the underlying database.
We will see this in action in our upcoming videos.

public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions[AppDbContext] options)
: base(options)
{
}

public DbSet[Employee] Employees { get; set; }
}

To be able to connect to a database we need the database connection string. In our next video, we will discuss, where do define the connection string and using it in Entity Framework Core.
Рекомендации по теме
Комментарии
Автор

Can't wait to see more!!! Grateful to have a teacher like you on YouTube. Thank You Sir Venkat.

hooiyan
Автор

Very clear and concise, you did a really good job of explaining this and focusing only on the lesson. This is how you teach.
Too many tutorials get bloated with creating a project, downloading this or that, or just simply get sidetracked.

Lucky.Sailor
Автор

Not only is the explanation outstanding, getting the syntax right would probably take me an entire day. Thank you for providing such an excellent service.

heatherbernstein
Автор

Looking forward to the upcoming videos. really love your teaching on EF. As perfect as always.
Again many thanks.

salomonks-francais
Автор

best teacher! May God always bless you!, always grateful to you.

jaynardrazon
Автор

Your videos are unbeatable, excellent, interactive. Thank you so much for your efforts to make us understand granular level concepts. You are awesome and please keep up good work.

pinkyabrol
Автор

You have a special place in heaven. Thank you!

barnabasyohannes
Автор

rememeber KUdvenkat includes source code in description and transcript as well. Best tutorials ever.Thank you kudvenkat

jeetenzhurlollz
Автор

God bless you sir, thank you so much enjoying this course.

solomoncharles
Автор

FINALLY A LOGICAL EXPLANATION THANK YOU SO MUCH

Nick-wztz
Автор

thanks alot you are great trainer, hope you all the best have a nice life

MmMm-tgmq
Автор

Great video. I learnt a lot from your tutorial videos. May I ask what is the best practice for using dbcontext class? Should we have multiple application dbcontext classes or just one dbcontext to manage the whole database?

reach
Автор

Looks like I am the first one to watch. Great Vdo man. Do u have detailed videos somewhere like Udemy?

namratakumar
Автор

Venkat bro your voice has gotten deeper since the last time i saw your videos, almost thought it wasn't you lol..thanks for the great video as always

tinashebandama
Автор

Please also consider making a video about managing disconnected entities. That would be really helpful. Thank you for this series.

reneschindhelm
Автор

Thank you very much for your great class, what do we do if we have to manage two different dbcontext, as I am handling a join query including tables from 2 dbs

adilhassan
Автор

Hi Venkat, Can you please explain how to map Employee or any other model classes to the context in dynamic way
ex: dbset<Tentity> T Entity.
I am trying to work with generic repository, where i wanted the map the model classes/entity types in generic way instead of static way.

srilathakagana
Автор

Common db or individual db is preferable when working as a team in entity framework?

jostinjose
Автор

So... what was the point of creating the 2nd probject in previous video if we end up doing it all in the same old project anyway?

get_ready
Автор

Will you register DB context as singleton or scope or Transient and why?

tejashreemadake