dbcontext in entity framework core

in this video we will discuss the significance of the dbcontext class in entity framework core

text version of the video
https://csharp-video-tutorials.blogspot.com/2019/04/dbcontext-in-entity-framework-core_23.html

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.
https://www.youtube.com/channel/uc7sewixm_yfamyonqcrgfwa/?sub_confirmation=1

slides
https://csharp-video-tutorials.blogspot.com/2019/04/dbcontext-in-entity-framework-core_21.html

asp.net core text articles & slides
https://csharp-video-tutorials.blogspot.com/2019/01/aspnet-core-tutorial-for-beginners.html

asp.net core tutorial
https://www.youtube.com/playlist?list=pl6n9fhu94yhvkdruslaqsferml_jh4xmu

angular, javascript, jquery, dot net & sql playlists
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

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.

dbcontext in entity framework core