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

Connect database to ASP.Net Core 2



Step 1: Create a database without migration

You can either create a database via CodeFirst under ASP.Net Core or


Codefirst uses Entitiy Framework Migration. At Codefirst, the database is created using Add Migration and Update Database.

add-migration "Initial_Database"


Update Database then creates a database object.

This database can be found under the SQL Server object in Visual Studio / Menue / View



Transfer the connection string

Wichig is the entry in the properties of the database. You have to copy this connection into the Startup.cs file and enter it.

Connection String :

Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Freiberufler_Datenbank;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False


Database First (description here)

Under Database First, the database is created directly or linked to an existing SQL Server database.

For larger applications, I recommend the Database-First method, as the data in a company always has priority and the software must never change structures in running databases.

Where: Never-touch a running system.

1a: create database:

To create an ASP.Net Core Server database manually, go to Visual Studio Menu / View / SQL Server Object Explorer

Then you can in SQL Server -> (localdb) \ MSSQLLocalDB-> Databases

Create a new local database with Add New Database



1b: Change to Database File


You can also put the database file directly into the project or put it in a suitable directory


1d) create tables

Then change to Server Explorer and Data Connections to the new database

In the new database you can create a table with / Tables-> Add New Table


1e: Table-> Create Fields

Then you create an ID field and some desired application fields

Under the ID field select Properties-> Identity Specification (Is Identity) to true


1f: Append

And then takes over the new table with Update Database


Example Code of the first table

CREATE TABLE [dbo].[tbl_Projects] (

    [IDProject] INT            IDENTITY (1, 1) NOT NULL,

    [URL]      NVARCHAR (255) NULL,

    [Title]    NVARCHAR (255) NULL,

    [Text]     NVARCHAR (MAX) NULL,

    [dtScan]   DATETIME       NULL,

    [dtList]   DATETIME       NULL,





Step 2) Add web application EF Entity Framework


Step 2a: Add SQL Client and EF Entity framework


First integrate the Entity Framework and SQL Server Client into the application

To do this, go to Menu-> Tools-> Nuget Package Manager -> Package Manager Console



Then run the following commands:


2a: Install Package Microsoft.EntityFrameworkCore.SqlServer      

2b: Install Package Microsoft.EntityFrameworkCore.Tools

2c: Install Package Microsoft.VisualStudio.Web.CodeGeneration.Design


For 2a: Install Package Microsoft.EntityFrameworkCore.SqlServer


2b: Install Package Microsoft.EntityFrameworkCore.Tools


And to 2c: Install Package Microsoft.VisualStudio.Web.CodeGeneration.Design



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));





Step 5: Use data in MVC

For the first time display, update, create and delete you can create a basic scaffolded item

For this, under Controllers-> Add-> New Item -> New Scaffolded Item ..



Then in the dialog: Add Scaffold select Installed-> MVC-> MVC Controller with views, using Entity Framework


Then in the dialog: Add MVC Controller withViews, using Entity Framework

If one chooses the model class of the movement data like here Model_Projects and under Data Context one chooses the data connection to the database




Ads on the web server:

Now if you start Debug line from Start Debug IIS Express in the debug line

And then enter the data model class after the localhost address, then the index should appear without IIS errors

URL like: http: // localhost: 51081 / Model_Project




🚩#23 ASP.Net: Add Web API Controller
🚩#11 Create and Connect a database to ASP.Net MVC Core 2
🚩# 23 ASP.Net: Adjust Validation
🚩#22 ASP.Net: Validation, validity
🚩#21 ASP.Net: Update Data, Save Edit View
# 20 Bind form data and attach to database
ASP.Net Core MVC: Insert dropdown list
ASP.Net: adapt the label
#17 ASP MVC: New form for creating an entry
EF: Specify default values ​​and Type Double and Bool from ASP.Net Model to SQL Server


Contact for Jobs, Project Requests: