[EFCore] 데이터를 임시로 삭제해보자 : Soft Delete (HasQueryFilter/IgnoreQueryFilter)

2022. 9. 6. 14:45·공부/EFCore
728x90

 

게임에서 아이템을 실수로 삭제하면 복구해주는 경우가 있다. 만약 우리가 아이템을 '삭제'한 게 정말 'Delete'를 의미했다면 복구가 불가능했을 것이다. 이럴 때 사용할 수 있는 게 바로 HasQueryFilter다.

 

public class Item
{
    public bool SoftDeleted { get; set; }
    //...//
}

 

Item 클래스에 SoftDeleted를 추가해준다. true가 임시 삭제를 의미하고, false가 기본값이다.

 

protected override void OnModelCreating(ModelBuilder builder)
{
    // 앞으로 Item Entity에 접근할 때 항상 사용되는 모델 레벨의 필터링
    // 필터를 무시하고 싶으면 IgnoreQueryFilter사용
    builder.Entity<Item>().HasQueryFilter(i => i.SoftDeleted == false);
}

 

AppDbContext에 새로운 함수를 추가해준다. 앞으로 Item을 삭제하고 싶으면 item.SoftDeleted = true; 를 해주면 된다. 이렇게 하면 Item 목록을 추출할 때 SoftDeleted가 true인 Item은 보여지지 않게 된다. SoftDeleted가 true인 아이들도 함께 보고 싶다면 IgnoreQueryFilter를 사용해주면 된다.

 

public static void ShowItems()
{
    using (AppDbContext db = new AppDbContext())
    {
        foreach (var item in db.Items.Include(i => i.Owner).IgnoreQueryFilters().ToList())
        {
            if (item.SoftDeleted)
            {
                Console.WriteLine($"DELETED - ItemId({item.ItemId}) TemplateId({item.TemplateId})");
            }
            else
            {
                if (item.Owner == null)
                    Console.WriteLine($"ItemId({item.ItemId}) TemplateId({item.TemplateId}) Owner(0)");
                else
                    Console.WriteLine($"ItemId({item.ItemId}) TemplateId({item.TemplateId}) Owner({item.Owner.PlayerId}) Owner({item.Owner.Name})");
            }
        }
    }
}

 

이런 식으로 Deleted Item도 삭제되지 않은 친구들과 함께 추출된다.

 

728x90

'공부 > EFCore' 카테고리의 다른 글

[EFCore] 다수의 Navigational Property가 같은 클래스를 참조하는 상황, 어떻게 대처할까  (0) 2022.09.07
[EFCore] Convention을 알아보자  (0) 2022.09.06
[EFCore] Relationship Data 수정 하기  (0) 2022.09.06
[EFCore] Pricipal 데이터가 없을 때 Dpendent 데이터가 존재할 수 있는가?  (0) 2022.09.05
[EFCore] Disconnected Update (Reload / Full Update)  (0) 2022.09.05
'공부/EFCore' 카테고리의 다른 글
  • [EFCore] 다수의 Navigational Property가 같은 클래스를 참조하는 상황, 어떻게 대처할까
  • [EFCore] Convention을 알아보자
  • [EFCore] Relationship Data 수정 하기
  • [EFCore] Pricipal 데이터가 없을 때 Dpendent 데이터가 존재할 수 있는가?
돌멩이수프
돌멩이수프
Information technology
  • 돌멩이수프
    WHAT DOES "IT" STAND FOR?
    돌멩이수프
  • 전체
    오늘
    어제
    • 분류 전체보기 (239)
      • 언어 (73)
        • html (3)
        • css (1)
        • java (6)
        • C (26)
        • C++ (2)
        • C# (29)
      • 공부 (7)
        • Unity (43)
        • 게임 서버 (26)
        • 네트워크 (5)
        • 데이터베이스 (7)
        • EFCore (19)
        • 기타 (14)
        • Git (5)
        • 운영체제 (1)
        • 소프트웨어공학 (21)
      • 2024-여름 (12)
      • 자기 관리 (4)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Entityfamework
    C
    C#
    디자인패턴
    tcp
    EFCore
    게임서버
    네트워크
    코딩
    coding
    백준
    HTML
    EntityFramework
    unity
    C언어
    java
    라즈베리파이
    Python
    자바
    유니티
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
돌멩이수프
[EFCore] 데이터를 임시로 삭제해보자 : Soft Delete (HasQueryFilter/IgnoreQueryFilter)
상단으로

티스토리툴바