{"id":2039,"date":"2023-06-06T16:47:49","date_gmt":"2023-06-06T09:47:49","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=2039"},"modified":"2023-06-06T16:47:49","modified_gmt":"2023-06-06T09:47:49","slug":"dbcontext","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/entity-framework-core-tutorial\/dbcontext\/","title":{"rendered":"DbContext"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about DbContext in EF Core and its important features.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the EF Core DbContext<\/h2>\n\n\n\n<p>In EF Core, the DbContext class works like a bridge between your application and the database. It is responsible for managing database connections and providing interfaces for interacting with the underlying database using strongly-typed C# objects.<\/p>\n\n\n\n<p>The DbContext class offers a set of functionalities that simplify common database operations such as inserting, updating, querying, and deleting data.<\/p>\n\n\n\n<p>The following highlight some of the keys feature of the DbContext class:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Database Context<\/h3>\n\n\n\n<p>The DbContext object represents a session with the database and serves as a container for all the entities that are being tracked. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Entity Mapping<\/h3>\n\n\n\n<p>The DbContext maps your domain model classes (also known as entities) to database tables. By defining DbSet properties within the DbContext, you can associate entity classes with specific database tables or views.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Change Tracking<\/h3>\n\n\n\n<p>The DbContext keeps track of changes made to the entities within its scope. It can detect additions, modifications, and deletions, which allows for efficient updates to the database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Querying<\/h3>\n\n\n\n<p>The DbContext provides methods to query the database using <a href=\"https:\/\/csharptutorial.net\/csharp-linq\/\">LINQ<\/a> (Language Integrated Query) syntax. You can write LINQ queries against DbSet properties to retrieve data from the corresponding tables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Transaction Management<\/h3>\n\n\n\n<p>The DbContext allows you to manage transactions when performing multiple database operations as a single unit of work. Via the DbContext&#8217;s interface, you can start, commit, or roll back transactions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Configuration<\/h3>\n\n\n\n<p>The DbContext allows you to define various aspects of how it interacts with the database. This includes specifying the database provider, connection string, and other important settings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use DbContext to simplify the database operations.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2039\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/entity-framework-core-tutorial\/dbcontext\/\"\n\t\t\t\tdata-post-title=\"DbContext\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2039\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/entity-framework-core-tutorial\/dbcontext\/\"\n\t\t\t\tdata-post-title=\"DbContext\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about DbContext in EF Core and its important features.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1970,"menu_order":23,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2039","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/2039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=2039"}],"version-history":[{"count":1,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/2039\/revisions"}],"predecessor-version":[{"id":2040,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/2039\/revisions\/2040"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1970"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=2039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}