How to Connect ASP.NET Core with SQL Server

IHUB Talent – The Best Full Stack .NET Training Institute in Hyderabad with Live Internship Program

If you're planning to build a rewarding career in software development, then IHUB is the perfect place to begin. Recognized as the best Full Stack .NET training institute in Hyderabad, IHUB offers a complete job-oriented course with a live intensive internship program, guided by real-time industry experts.

Whether you're a graduate, postgraduate, someone with an educational gap, or planning a job domain change, IHUB’s Full Stack .NET course is designed to make you job-ready in just a few months. The program is structured for freshers and working professionals alike, offering hands-on experience and placement support.

🔹 Why Choose IHUB for Full Stack .NET Training?

Expert Faculty: Learn from certified trainers with real-world industry experience.

Live Intensive Internship: Work on actual projects under the guidance of professionals.

Placement Assistance: Resume preparation, mock interviews, and job referrals.

Suitable for All Backgrounds: Freshers, students with gaps, or those changing job domains.

Flexible Learning: Online and offline training options available.

Project-Based Training: Gain hands-on experience by developing end-to-end real-time projects.

How to Connect ASP.NET Core with SQL Server

Connecting ASP.NET Core with SQL Server is a key step in developing modern web applications. ASP.NET Core provides built-in support for integrating databases using Entity Framework Core (EF Core), a lightweight and powerful ORM (Object-Relational Mapper). Here's a simple guide to get you started.

Step 1: Install Required Packages

In your ASP.NET Core project, install the EF Core SQL Server provider via NuGet Package Manager or CLI:

bash

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

You’ll also need the tools package for migrations:

bash

dotnet add package Microsoft.EntityFrameworkCore.Tools

Step 2: Configure Your Database Context

Create a new class that inherits from DbContext:

csharp

public class AppDbContext : DbContext

{

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

    public DbSet<Product> Products { get; set; }

}

Step 3: Add Connection String in appsettings.json

json

"ConnectionStrings": {

  "DefaultConnection": "Server=.;Database=MyAppDb;Trusted_Connection=True;"

}

Step 4: Register DbContext in Program.cs

csharp

builder.Services.AddDbContext<AppDbContext>(options =>

    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

Step 5: Run Migrations and Update Database

Use these commands to create and apply the database:

bash

dotnet ef migrations add InitialCreate  

dotnet ef database update

Conclusion

With just a few steps, your ASP.NET Core app is now connected to SQL Server. This setup enables you to perform CRUD operations seamlessly using EF Core.

Read More

Logging in .NET Core using Serilog/NLog

Error Handling in .NET Core Applications

Authentication & Authorization in ASP.NET Core

LINQ Queries in C# – Basics and Example

Visit Our I-HUB Talent Training Institute Hyderabad 

Comments

Popular posts from this blog

Why Choose .NET for Full Stack Development?

What is Full Stack .NET Development?

What is ASP.NET Core?