[C#] EF Core CRUD

참고


소개

  • 앞서 Code-First 방식으로 EF Core 첫 번째 어플리케이션을 만들어 보았습니다.
  • 다음으로 만들어진 프로젝트에 이어서, CRUD 기능을 추가해 보도록 하겠습니다.

Insert Data

  • 앞서, School, Course 테이블을 마이그레이션 하여 PostgreSQL 데이터베이스에 생성하였습니다.
  • 그럼 실제로 School 테이블에 데이터를 저장해 보도록 하겠습니다.
using EFCoreSample;

using (var context = new SchoolContext())
{
    context.Database.EnsureCreated();
    AddStudent(context);
}

static void AddStudent(SchoolContext context)
{
    var student = context.Students.FirstOrDefault();
    if (student != null) return;

    context.Students.Add(new Student
    {
        StudentId = 1,
        Name = "JoBeomHee"
    });

    context.Students.Add(new Student
    {
        StudentId = 2,
        Name = "BeomBeomJoJo"
    });

    context.SaveChanges();
}
  • PostgreSQL 에서 Student 테이블을 직접 확인 결과, 2개의 데이터가 정상적으로 Insert 된 것을 확인할 수 있습니다.


Update Data

  • 다음은 앞서 추가했던 데이터에서 이름이 "BeomBeomJoJo" 인 사람의 이름을 "Kim" 으로 변경해 보도록 하겠습니다.
using EFCoreSample;

using (var context = new SchoolContext())
{
    context.Database.EnsureCreated();
    UpdateStudent(context);
}

static void UpdateStudent(SchoolContext context)
{
    var student = context.Students.Where(x => x.Name == "BeomBeomJoJo").FirstOrDefault();
    student.Name = "Kim";
    context.SaveChanges();
}
  • 실행 결과, "BeomBeomJoJo" 이름에서 "Kim" 으로 변경된 것을 확인할 수 있습니다.


Delete Data

  • 다음으로는 데이터를 삭제해 보도록 하겠습니다.
  • 이름이 "Kim" 이라는 학생을 조회하여 해당 학생을 student 테이블에서 삭제해 보도록 하겠습니다.
using EFCoreSample;

using (var context = new SchoolContext())
{
    context.Database.EnsureCreated();
    DeleteStudent(context);
}

static void DeleteStudent(SchoolContext context)
{
    var student = context.Students.Where(x => x.Name == "Kim").FirstOrDefault();
    context.Students.Remove(student);

    // or
    // context.Remove<Student>(student);

    context.SaveChanges();
}
  • 실행 결과, 삭제 된 것을 확인할 수 있습니다.


전체 소스 코드

  • program.cs 의 전체 소스코드는 다음과 같습니다.
using EFCoreSample;

using (var context = new SchoolContext())
{
    context.Database.EnsureCreated();
    DeleteStudent(context);
}

static void AddStudent(SchoolContext context)
{
    var student = context.Students.FirstOrDefault();
    if (student != null) return;

    context.Students.Add(new Student
    {
        StudentId = 1,
        Name = "JoBeomHee"
    });

    context.Students.Add(new Student
    {
        StudentId = 2,
        Name = "BeomBeomJoJo"
    });

    context.SaveChanges();
}

static void UpdateStudent(SchoolContext context)
{
    var student = context.Students.Where(x => x.Name == "BeomBeomJoJo").FirstOrDefault();
    student.Name = "Kim";
    context.SaveChanges();
}

static void DeleteStudent(SchoolContext context)
{
    var student = context.Students.Where(x => x.Name == "Kim").FirstOrDefault();
    context.Students.Remove(student);

    // or
    // context.Remove<Student>(student);

    context.SaveChanges();
}
728x90

'C#' 카테고리의 다른 글

[C#] EF Core 첫 번째 어플리케이션  (0) 2023.03.15
[C#] Entity Framework Core 란  (1) 2023.03.14
[C#] EF Core - DbContext  (0) 2023.03.11
[C#] EF Core - Fluent API  (0) 2023.03.10
[C#] EF Core - Configurations  (0) 2023.03.09

이 글을 공유하기

댓글

Designed by JB FACTORY