How should I structure my Entity Framework access/query classes

David Thielen 2,281 Reputation points
2023-03-11T19:43:24.1166667+00:00

I've placed all my EF model classes that represent my tables in the Models folder along with my DbContext class - as recomended.

I now want to create a class for each table that access the DB using DbContext and the appropriate model class. This is the code to create a new row, query a row based on search criteria, update a row, delete a row, etc. I don't want queries spread all over my code and instead feed all DB access trough these access classes.

First off, is this a good approach? Or is my ADO.NET experience taking my in a sub-optimal direction? And if this approach is sub-optimal, what is optimal?

Second, assuming this is a good approach, what folder should I put these in?

Third, is there a class these classes should inherit from, a library they should use (like possibly Specification), suggested ways a search filter should be crafted/passed (like possibly an Expression), or anything else I should build these classes around?

thanks - dave

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2023-03-13T05:58:29.07+00:00

    @David Thielen, Welcome to Microsoft Q&A, If you don't want queried spread all over my code, I recommend that you could create a class to include these methods.

    Here is a code example you could refer to.

    Model:

     public class Student
        {
            public int Id { get; set; } 
    
            public string? Name { get; set; }    
    
            public string? Description { get; set; }
        }
        public class StuContext:DbContext
        {
            public DbSet<Student> Students { get;set;}
    
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                string str = "connstr";
                optionsBuilder.UseSqlServer(str);
            }
        }
    
    

    EFHelper

     public class EFHelper
        {
    
            public void Insert(StuContext context,Student student)
            {
                context.Students.Add(student));
                context.SaveChanges() ;
            }
    
            public List<Student> GetById(StuContext context,int id)
            {
                var result=from m in context.Students where m.Id == id
                           select m ;
                return result.ToList() ;
            }
    
            public void DeleteById(StuContext context, int id)
            {
                var result = from m in context.Students
                             where m.Id == id
                             select m;
                context.Students.RemoveRange(result) ;  
                context.SaveChanges();
            }
    
            public void UpdateById(StuContext context, int id,string name)
            {
                var result = from m in context.Students
                             where m.Id == id
                             select m;
                foreach (var item in result)
                {
                    item.Name = name;
                }
                context.SaveChanges();
            }
        }
    
    

    You could try to use the above EFHelper class to do the CRUD operations in ef core. However, I find that it may be a flaw that you need to define a very large number of overloaded functions if you need to delete, update or modify according to different conditions.

    Hope my answer could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Karen Payne MVP 35,036 Reputation points
    2023-03-13T23:48:45.2533333+00:00

    I place all my EF Core code in a class project so that the code can be used in more than one project e.g

    EF1

    Then utility code in other class project

    EF2

    So each project uses the above projects and has code in these projects specific to the business logic of the project.