ASP.NET Core MVC 데이터베이스 프로그래밍
ASP.NET Core MVC 데이터베이스 프로그래밍 시작
이 문서는 ASP.NET Core MVC에서 데이터베이스에 접근하는 세 가지 방식(ADO.NET, Dapper, EF Core)을 단계별로 실습하면서 비교할 수 있도록 구성했습니다.
다음 경로의 ADO.NET 데이터베이스 프로그래밍 강좌의 내용을 알고 있다고 가정합니다.
실습에서는 DotNetNote 데이터베이스를 대상으로, 하나의 컨트롤러 파일 내에 직접 연결 문자열을 지정해 동작하도록 구성합니다. 이후 단계의 데이터베이스 프로그래밍부터는 appsettings.json에 연결 문자열을 설정하고 Program.cs에서 DI(종속성 주입)를 적용하는 표준 방식으로 진행할 예정입니다.
NOTE
이번 강좌에서는 MVC 뷰를 별도로 만들지 않고 컨트롤러에서 바로 JSON으로 결과를 확인하여, 쿼리부터 모델 매핑까지의 과정을 가장 단순한 경로로 검증하고 각 데이터 접근 방식의 차이에 집중합니다.
0. 준비물
이미 사용중인 DotNetNote 솔루션이 있으면 그곳에 다음 내용을 추가하면서 진행합니다.
1. MVC 프로젝트 만들기
dotnet new mvc -n DotNetNote
2. NuGet 패키지 설치
cd DotNetNote
dotnet add package Microsoft.Data.SqlClient
dotnet add package Dapper
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
3. DotNetNote.SqlServer 데이터베이스 프로젝트 만들기
Visual Studio에서 새 프로젝트 → 템플릿: SQL Server Database Project → 이름: DotNetNote.SqlServer
프로젝트가 생성되면 Books.sql을 추가하고 다음을 입력합니다.
CREATE TABLE [dbo].[Books]
(
[Id] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Name] NVARCHAR(100) NOT NULL
);
3-1. Post-Deployment 스크립트로 기본 데이터 자동 삽입(권장)
데이터베이스 게시 시, 테이블이 없다면 만들고 샘플 데이터 2건을 자동으로 넣습니다.
DotNetNote.SqlServer프로젝트에서 Post-Deployment 스크립트를 추가합니다. 파일명 예: Script.PostDeployment.sql (프로젝트 루트 또는Post-Deployment노드)아래 내용을 스크립트에 넣습니다.
-- Script.PostDeployment.sql
-- 1) Books 테이블이 없으면 생성
IF OBJECT_ID('dbo.Books', 'U') IS NULL
BEGIN
CREATE TABLE dbo.Books
(
Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Name NVARCHAR(100) NOT NULL
);
END
-- 2) 샘플 데이터 2건을 존재할 때는 건너뛰고, 없으면 삽입
IF NOT EXISTS (SELECT 1 FROM dbo.Books WHERE Name = N'C# 교과서')
INSERT INTO dbo.Books (Name) VALUES (N'C# 교과서');
IF NOT EXISTS (SELECT 1 FROM dbo.Books WHERE Name = N'ASP.NET & Core를 다루는 기술')
INSERT INTO dbo.Books (Name) VALUES (N'ASP.NET & Core를 다루는 기술');
NOTE
SSDT(Database Project)에서는 프로젝트 속성이나 Publish 프로필에서 Post-Deployment Script로 해당 파일을 지정하면 게시 시 자동으로 실행됩니다.
4. DotNetNote 데이터베이스에 게시(Publish)
DotNetNote.SqlServer 프로젝트에서 Publish... 실행
- Target Database Connection: LocalDB 또는 지정한 SQL Server 인스턴스
- Database name:
DotNetNote - Publish 클릭
게시가 완료되면 DotNetNote DB에 Books 테이블이 생성되고,
위 Post-Deployment 스크립트에 의해 샘플 데이터 2건도 자동으로 들어갑니다.
5. 컨트롤러 파일 추가
Controllers 폴더에 DatabaseController.cs 파일을 생성합니다.
1단계: ADO.NET으로 조회하기
가장 기본적인 방식입니다. /database/ado로 JSON 결과를 확인합니다.
코드: DotNetNote/Controllers/DatabaseController.cs
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
namespace DotNetNote.Controllers
{
[ApiController]
[Route("database")]
public class DatabaseController : ControllerBase
{
private const string ConnectionString =
@"Server=(localdb)\MSSQLLocalDB;Database=DotNetNote;Trusted_Connection=True;";
public class Book
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
[HttpGet("ado")]
public async Task<IActionResult> GetByAdoNet()
{
var list = new List<Book>();
await using var conn = new SqlConnection(ConnectionString);
await conn.OpenAsync();
const string sql = "SELECT Id, Name FROM dbo.Books ORDER BY Id;";
await using var cmd = new SqlCommand(sql, conn);
await using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection);
while (await reader.ReadAsync())
{
list.Add(new Book
{
Id = reader.GetInt32(0),
Name = reader.GetString(1)
});
}
return Ok(list); // JSON 반환
}
}
}
테스트:
GET http://localhost:5000/database/ado
2단계: Dapper로 조회하기
같은 컨트롤러에 /database/dapper를 추가합니다. 코드가 한결 간결해집니다.
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Dapper;
namespace DotNetNote.Controllers
{
[ApiController]
[Route("database")]
public class DatabaseController : ControllerBase
{
private const string ConnectionString =
@"Server=(localdb)\MSSQLLocalDB;Database=DotNetNote;Trusted_Connection=True;";
public class Book
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
// ADO.NET
[HttpGet("ado")]
public async Task<IActionResult> GetByAdoNet()
{
var list = new List<Book>();
await using var conn = new SqlConnection(ConnectionString);
await conn.OpenAsync();
const string sql = "SELECT Id, Name FROM dbo.Books ORDER BY Id;";
await using var cmd = new SqlCommand(sql, conn);
await using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection);
while (await reader.ReadAsync())
{
list.Add(new Book
{
Id = reader.GetInt32(0),
Name = reader.GetString(1)
});
}
return Ok(list);
}
// Dapper
[HttpGet("dapper")]
public async Task<IActionResult> GetByDapper()
{
await using var conn = new SqlConnection(ConnectionString);
var books = await conn.QueryAsync<Book>(
"SELECT Id, Name FROM dbo.Books ORDER BY Id;");
return Ok(books); // JSON 반환
}
}
}
테스트:
GET http://localhost:5000/database/ado
GET http://localhost:5000/database/dapper
3단계: EF Core로 조회하기
ORM 접근입니다. DbContext를 같은 파일에 정의하고 /database/ef로 확인합니다.
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Dapper;
namespace DotNetNote.Controllers
{
[ApiController]
[Route("database")]
public class DatabaseController : ControllerBase
{
private const string ConnectionString =
@"Server=(localdb)\MSSQLLocalDB;Database=DotNetNote;Trusted_Connection=True;";
public class Book
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
// EF Core용 경량 DbContext
public class BooksDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(ConnectionString);
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>(e =>
{
e.ToTable("Books");
e.HasKey(x => x.Id);
e.Property(x => x.Name).IsRequired().HasMaxLength(100);
});
}
public DbSet<Book> Books => Set<Book>();
}
[HttpGet("ado")]
public async Task<IActionResult> GetByAdoNet()
{
var list = new List<Book>();
await using var conn = new SqlConnection(ConnectionString);
await conn.OpenAsync();
const string sql = "SELECT Id, Name FROM dbo.Books ORDER BY Id;";
await using var cmd = new SqlCommand(sql, conn);
await using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection);
while (await reader.ReadAsync())
{
list.Add(new Book
{
Id = reader.GetInt32(0),
Name = reader.GetString(1)
});
}
return Ok(list);
}
[HttpGet("dapper")]
public async Task<IActionResult> GetByDapper()
{
await using var conn = new SqlConnection(ConnectionString);
var books = await conn.QueryAsync<Book>(
"SELECT Id, Name FROM dbo.Books ORDER BY Id;");
return Ok(books);
}
[HttpGet("ef")]
public async Task<IActionResult> GetByEfCore()
{
await using var db = new BooksDbContext();
var books = await db.Books
.OrderBy(b => b.Id)
.AsNoTracking()
.ToListAsync();
return Ok(books); // JSON 반환
}
}
}
테스트:
GET http://localhost:5000/database/ado
GET http://localhost:5000/database/dapper
GET http://localhost:5000/database/ef
마무리
이제 동일 테이블에 대해 세 가지 방식으로 결과를 확인할 수 있습니다.
| 기술 | 경로 | 특징 |
|---|---|---|
| ADO.NET | /database/ado |
기본 API로 세밀한 제어, 코드 길어짐 |
| Dapper | /database/dapper |
경량 ORM, 빠르고 간결 |
| EF Core | /database/ef |
정식 ORM, 추상화/유지보수/확장성 유리 |
NOTE
필자는 앞서 소개한 ADO.NET, Dapper, EF Core 관련 코드를 지금도 별다른 참고 자료 없이 직접 작성할 수 있습니다. 예전에는 그런 방식으로 익히는 것이 자연스러웠고, 실제로 그렇게 학습해야만 했던 시절이었습니다.
하지만 이제는 시대가 달라졌습니다. 처음 학습을 시작하시는 분들이라면 이러한 코드를 완전히 암기하거나 처음부터 직접 작성하는 것이 다소 낯설고 어렵게 느껴질 수 있습니다. 그런 경우에는 ChatGPT와 같은 AI 도구를 적극적으로 활용하여 코드를 생성하고, 이를 수정·이해하면서 배우는 것도 매우 좋은 학습 방법입니다.
즉, 처음부터 완벽히 외우려 하기보다는 도구의 도움을 받아 흐름을 이해하고 구조를 익히는 것이 더 효율적인 접근법입니다.