C# 응용프로그램을 사용하면서 데이터베이스와 연동해야하는 경우, SQL Script 를 코드에 입력하여 사용할 수도 있지만 좀 더 고급적인 EntityFrameworkCore 기술이 있다.
Class 생성만으로 데이터베이스, 테이블을 생성할 수 도 있고, CRUD 를 Class 객체를 이용 가능하다.
1. 프로젝트 생성
2. Nuget Package 설치
- EntityFrameworkCore : EntityFrameworkCore 를 사용하여 CRUD 가 가능
- EntityFrameworkCore.Tools : Code 를 작성하고 패키지 관리자 콘솔을 이용하여 데이터베이, 테이블을 만들 수 있다.
- Npsql.EntityFrameworkCore.PostgreSQL : PostgreSQL 을 이용하기 위한 패키지
* PostgreSQL 이 아닌 다른 Database 를 이용한다면 그에 맞는 패키지를 설치하면 된다.
3. Class Entity 생성
public class Languages
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Korean { get; set; }
public string English { get; set; }
}
Id 는 Primary Key, INTEGER 타입으로 만들어지며
Name, Korean, English 는 TEXT 타입으로 테이블이 생성할 계획이다.
4. DbContext Class 생성
public class EFCoreDbContext : DbContext
{
public DbSet<Languages> Languages { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Host=127.0.0.1;Port=5432;Database=SampleDatabase;Username=postgres;Password=123!");
}
}
사용하기 위한 Languages Property
override OnConfiguring 함수는 Database 에 접속하기 위한 코드이다.
5. 코드를 작성하고 Database, Table 을 생성하는 법 (Code First 기법)
우선 EntityFrameworkCore.Tools 이 설치되어 있는지 확인합니다.
그리고 해당 프로젝트를
- 시작 프로젝트로 설정하고
- 패키지 관리자 콘솔에서도 기본 프로젝트로 설정한다.
- get-help entityframeworkcore 를 입력하여 도움말을 볼 수 있다.
PM> get-help entityframeworkcore
_/\__
---==/ \\
___ ___ |. \|\
| __|| __| | ) \\\
| _| | _| \_/ | //|\\
|___||_| / \\\/\\
TOPIC
about_EntityFrameworkCore
SHORT DESCRIPTION
Provides information about the Entity Framework Core Package Manager Console Tools.
LONG DESCRIPTION
This topic describes the Entity Framework Core Package Manager Console Tools. See https://docs.efproject.net for
information on Entity Framework Core.
The following Entity Framework Core commands are available.
Cmdlet Description
-------------------------- ---------------------------------------------------
Add-Migration Adds a new migration.
Bundle-Migration Creates an executable to update the database.
Drop-Database Drops the database.
Get-DbContext Lists and gets information about available DbContext types.
Get-Migration Lists available migrations.
Optimize-DbContext Generates a compiled version of the model used by the DbContext.
Remove-Migration Removes the last migration.
Scaffold-DbContext Scaffolds a DbContext and entity types for a database.
Script-DbContext Generates a SQL script from the DbContext. Bypasses any migrations.
Script-Migration Generates a SQL script from migrations.
Update-Database Updates the database to a specified migration.
SEE ALSO
Add-Migration
Bundle-Migration
Drop-Database
Get-DbContext
Get-Migration
Optimize-DbContext
Remove-Migration
Scaffold-DbContext
Script-DbContext
Script-Migration
Update-Database
- 여러가지 기능이 있지만 필요한 기능만 알아보겠습니다.
* '~~~' 는 커스텀 커밋 내용을 입력하는 곳 입니다.
* 명령을 사용하기 위해선 프로젝트 빌드 오류가 없어야 합니다.
Get-DbContext : 현재 DbContext 를 상속 받는 Class 의 DbSet 리스트를 보여줍니다.
Add-Migration '~~~' : 코드 변경 후 실제 데이터베이스 업데이트 전 커밋하는 개념
Get-Migration '~~~' : 현재 커밋된 기록을 볼 수 있습니다.
Remove-Migration '~~~' : 최근 커밋된 Migration 을 삭제합니다.
Update-Database : 커밋된 Migration 을 실제 데이터베이스에 업데이트 하는 행위 입니다.
- Add-Migration Init 을 입력하여 만든 Languages Entity 를 커밋합니다.
- Update-database 를 입력하여 실제 데이터베이스를 업데이트 합니다.
'정보 공유 > Nuget' 카테고리의 다른 글
EntityFrameworkCore 사용법 (2/2) (0) | 2024.06.11 |
---|---|
log4net 사용법 (0) | 2024.06.10 |