Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

Asp.Net Mvc: DbContext as interface of a SQL database to the Asp.Net Core application

 

A connected database is always connected to Asp.Net Core Mvc with some kind of bridge or interface.

This is called DbContext. In principle, the Entity Framework DbContext translates a SQL Server table into a local DataModel class of an Asp.Net application.

 

DbContext is the connection to the database. And below that are the dbSet tables, which are intermediate translated tables to the database

 

 

 

Step 3) Add DBContext

DBContext is the interface from the database to the application

Add a new class in the directory / Models or / _Database with Add / New Item

 

Enter a name for the context file under the dialog

 

In the class you have to describe the interface of ASP.Net Core 2 to the database.

Insert from

//< Using>

using Microsoft.EntityFrameworkCore;

//</ Using>

 

Then name the class in the likeness and derive it from the type: DBContext

public class app_Database_Context : DbContext

 

Then insert the following public description of the connection so that you can use it in the application.

public app_Database_Context(DbContextOptions<app_Database_Context> options)

            : base(options)

        { }

 

The final step is to rewrite the local data model class to the table in the database

public DbSet<Models.Model_Project> tbl_Projects { get; set; }

 

 

Complete code of my file: app_Database_Context.cs, which I put under / _Database.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

//< Using>

using Microsoft.EntityFrameworkCore;

//</ Using>

 

namespace Freiberufler

{

    public class app_Database_Context : DbContext

    {

        public app_Database_Context(DbContextOptions<app_Database_Context> options)

            : base(options)

        { }

 

        #region EF: Datase-Tabels to Models

        //------------< region: Datase-Tables to Models >------------

        public DbSet<Models.Model_Project> tbl_Projects { get; set; }

 

        //------------</ region : Datase-Tables to Models >------------

        #endregion /EF: Datase-Tabels to Models

 

    }

}

 

 

 

Step 4: Customize Startup.cs -ConfigureServices

In the Startup.cs file you have to insert the use of the database as a service.

To do this, insert the line with the connection.

 

        public void ConfigureServices(IServiceCollection services)

        {

            var connection = @"Server=(localdb)\mssqllocaldb;Database=Freiberufler_Datenbank;Trusted_Connection=True;";

            services.AddDbContext<app_Database_Context>(options => options.UseSqlServer(connection));

            services.AddMvc();

        }

 

 

 

 

Mobile
»
Asp MVC: Format Date as Short Date
»
Asp.Net Core: How to get the ApplicationDbContext in an Asp.Net Core MVC application
»
Asp.Net MVC: retrieve data via SQLClient from SQL Server without EF Entity Framwork ApplicationDbContext
»
Asp.Net Mvc: DbContext as interface of a SQL database
»
EF: Update Migration Type Mappings. Which C # variable types are to which SQL server field types
»
ASP.Net Core: Create a database from a model class

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de