SoFunction
Updated on 2025-03-08

Example of REST method for C# implementing general addition, deletion, modification and search based on DBContext(EF)

This article describes the REST method of C# using DBContext (EF) to implement universal addition, deletion, modification and search. It is shared with you for your reference. The details are as follows:

After we use Entity Data Model to generate entity classes, we generally perform basic additions, deletion, modification and search operations on these classes. If each class has to write these basic methods, it is too boring. The following is a step by step to introduce how to use DBContext to implement general addition, deletion, modification and query, as well as problems that are prone to occur during the implementation process.

1. Open vs2012 and create a new class library project

2. Add a new Entity Data Model item to this item

3. Open, modify res://* to res://yourproject

Otherwise, the following error will be reported:

(3,4) : error 0019: Each type name in a schema must be unique. Type name '' was already defined.

4. Build this project

5. Create another project for web api

MVC 4 Web Application –> Web API Templates

Note that the EF version of this project should be consistent with the previous project version

6. Add a class to Models:

Copy the codeThe code is as follows:
public class GenericDBContext<T> : WifiEntities where T : class
{
        public DbSet<T> Items { get; set; }
        public List<T> Get()
        {
            return Set<T>().ToList();
        }

        public T Get(int id)
        {
            return (id);
        }

        public void Put(T item)
        {
            (item);
            Entry(item).State = ;
            SaveChanges();
        }

        public void Post(T item)
        {
            (item);
            SaveChanges();
        }

        public void Delete(int id)
        {
            Delete(Get(id));
        }

        public void Delete(T item)
        {
            (item);
            Entry(item).State = ;
            SaveChanges();
        }
}

7. Add one to Controllers below:

Copy the codeThe code is as follows:
public class GenericController<T> : ApiController where T : class
{
        private readonly GenericDBContext<T> _context = new GenericDBContext<T>();

        public List<T> Get()
        {
            return _context.Get();
        }

        public T Get(int id)
        {
            return _context.Get(id);
        }

        public void Post([FromBody]T t)
        {
            _context.Post(t);
        }

        public void Put([FromBody]T t)
        {
            _context.Put(t);
        }

        public void Delete(int id)
        {
            _context.Delete(id);
        }
}

At this point, the general method is finished

8. You can write a specific controller below

Copy the codeThe code is as follows:
public class ADController : GenericController<AD>
{

}

9. Finally, use soapuui for debugging, and pass

I hope this article will be helpful to everyone's C# programming.