---
title: "How do you establish a database connection in an ASP.NET MVC project?"  
description: "How do you establish a database connection in an ASP.NET MVC project?"  
author: "Revati S Misra"  
published: 2023-06-11  
updated: 2023-06-14  
canonical: https://www.mindstick.com/forum/158706/how-do-you-establish-a-database-connection-in-an-asp-dot-net-mvc-project  
category: "asp.net mvc"  
tags: ["asp.net mvc", "database connection"]  
reading_time: 3 minutes  

---

# How do you establish a database connection in an ASP.NET MVC project?

How do you establish a [database connection](https://www.mindstick.com/forum/159617/how-to-connect-to-a-database-connection-in-java) in an [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) [project](https://www.mindstick.com/articles/105927/how-to-excel-at-managing-multiple-projects)?

## Replies

### Reply by Aryan Kumar

To establish a [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) [connection](https://www.mindstick.com/articles/13012/what-makes-ethernet-connection-better-than-wifi) in an [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) [MVC project](https://www.mindstick.com/forum/158621/what-is-a-null-reference-exception-and-how-can-it-occur-in-an-asp-dot-net-mvc-project), you need to:

1. Create a connection string in the project's web.config file.
2. Create a database context class that inherits from DbContext.
3. Initialize the database context in the application's Startup class.
4. Use the database context to interact with the database.

Here are the steps in more detail:

1. Create a connection string in the project's web.config file.

The connection string is a text file that contains information about the database server, database name, and user credentials. To create a connection string, open the web.config file in a text editor and add the following section:

<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True" /> </connectionStrings>

The name of the connection string is "DefaultConnection". The connection string specifies that the database server islocalhost, the database name is MyDatabase, and the user is authenticated using Windows Integrated Security.

1. Create a database context class that inherits from DbContext.

The database context class is a class that provides access to the database. To create a database context class, create a new class in the project and have it inherit from DbContext. For example:

Code snippet

```plaintext
public class MyContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }
}
```

The MyContext class has a DbSet property called Customers. The DbSet property represents a collection of Customer objects in the database.

1. Initialize the database context in the application's Startup class.

The application's Startup class is responsible for initializing the application's dependencies. To initialize the database context, add the following code to the Startup class's ConfigureServices method:

Code snippet

```plaintext
services.AddDbContext<MyContext>(options =>
{
    options.UseSqlServer("DefaultConnection");
});
```

The AddDbContext method tells the dependency injection container to create a new instance of the MyContext class and configure it to use the connection string named "DefaultConnection".

1. Use the database context to interact with the database.

Once the database context has been initialized, you can use it to interact with the database. For example, you can use the following code to retrieve all of the customers from the database:

Code snippet

```plaintext
var customers = context.Customers.ToList();
```

The ToList() method returns a list of all of the Customer objects in the database.

You can also use the database context to insert, update, and delete Customer objects from the database.


---

Original Source: https://www.mindstick.com/forum/158706/how-do-you-establish-a-database-connection-in-an-asp-dot-net-mvc-project

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
