1

나는 저장소 패턴 IN 절을 구현하는 방법에 대한 몇 가지 도움을 찾고. 각각의 레코드를 한 번 호출하는 대신 ID 집합을 가져와이 ID를 Context에 전달하여 EF가있는 Repository Pattern을 사용하여 조건을 만족하는 엔티티를 가져옵니다. 엔티티 프레임 워크는

context.Students.Where(x => StudentIDs.contains(x.ID)) 

어떻게 DB에 단일 통화와 저장소 층 또는 패턴 같은 구현하는

:

나는 우리가 이런 식으로 뭔가를 할 수 있습니다 알고 있었다? 당신은 정말 순수 주의자 경우

+0

가 그냥 된 IQueryable를 노출합니다. – Aron

+0

그리고이 컨텍스트는 어떻게됩니까? 학생. 저장소 (x => StudentIDs.contains (x.ID))가 저장소 패턴과 충돌합니까? – mr100

답변

0

는, 그래 당신은 추상화 DbContext 완전히 당신이 암시하는 것처럼 보인다해야한다.

나는 내가 완전히 문제를 이해 모르겠지만, 그런 일이 작업을 수행해야합니다

namespace EFRepo 
{ 
    class Student 
    { 
     public long Id { get; set; } 
     public string Name { get; set; } 
    } 

    class SchoolContext : DbContext 
    { 
     public DbSet<Student> Students { get; set; } 
    } 

    class SchoolRepository 
    { 
     private SchoolContext context = new SchoolContext(); 

     public Student Add(string name) 
     { 
      Student student = new Student { Name = name }; 

      context.Students.Add(student); 

      context.SaveChanges(); 

      return student; 
     } 

     public IEnumerable<Student> GetStudentsByIds(IEnumerable<long> ids) 
     { 
      return context.Students.Where(x => ids.Contains(x.Id)); 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      SchoolRepository repo = new SchoolRepository(); 

      repo.Add("Bully"); 
      repo.Add("Crawler"); 
      repo.Add("Tart"); 

      foreach (Student s in repo.GetStudentsByIds(new[] { 1L, 3 })) 
      { 
       Console.WriteLine(s.Name); 
      } 
     } 
    } 
}