zoukankan      html  css  js  c++  java
  • 用DBContext (EF) 实现通用增删改查的REST方法

    我们用ADO.NET Entity Data Model来生成实体类后,一般都会对这些类进行基本的增删改查操作,如果每个类都要写这些基本的方法,实在太乏味了。下面就是通过step by step的方式介绍如何用DBContext来实现通用增删改查的REST方法,以及实现过程中容易出现的问题。

    1. 打开vs2012,新建一个class library的项目

    2. 新加一个ADO.NET Entity Data Model项到这个项目

    3. 打开App.Config, 修改res://* 为res://yourproject

    否则会报下面的这咱错误:

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

    4. Build这个项目

    5. 新建另一个web api的项目

    ASP.NET MVC 4 Web Application –> Web API 模板

    注意这个项目的EF的版本与上个项目的版本要一致

    6. 添加一个类到Models下:

    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 Items.Find(id);
            }
    
            public void Put(T item)
            {
                Items.Attach(item);
                Entry(item).State = EntityState.Modified;
                SaveChanges();
            }
    
            public void Post(T item)
            {
                Items.Add(item);
                SaveChanges();
            }
    
            public void Delete(int id)
            {
                Delete(Get(id));
            }
    
            public void Delete(T item)
            {
                Items.Attach(item);
                Entry(item).State = EntityState.Deleted;
                SaveChanges();
            }
        }

    7. 添加一个到Controllers下面:

    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);
            }
        }

    至此,通用方法写完了

    8. 下面就可以写具体Controller了

    public class ADController : GenericController<AD>
        {
          
        }

    9. 最后用soap ui 进行调试,通过

  • 相关阅读:
    CentOS下date命令
    spring-data-redis --简单的用spring-data-redis
    Unable to Rebuild JIRA Index
    JIRA Cannot Start Due to 'unable to clean the cache directory: /opt/jira/plugins/.osgi-plugins/felix'
    Java compiler level does not match the version of the installed Java project facet.
    maven scope含义的说明
    maven 下载 源码和javadoc命令
    Redis 入门第一发
    mysql 1194 – Table ‘tbl_video_info’ is marked as crashed and should be repaired 解决方法
    tomcat用redis做session共享
  • 原文地址:https://www.cnblogs.com/fengwenit/p/3793115.html
Copyright © 2011-2022 走看看