EF Api 全套

using MoNiLianXi_Api.EF;
using MoNiLianXi_Api.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace MoNiLianXi_Api.Controllers
{
    public class DefaultController : ApiController
    {
        //ef显示
        [Route("api/Search")]
        [HttpGet]
        public IHttpActionResult Search()
        {
            List<RedPacketViewModel> list = new List<RedPacketViewModel>();
            using (var ef = new Model1())
            {
                list = (from s in ef.RedPacket
                        select new RedPacketViewModel
                        {
                            ID = s.ID,
                            ActivityName = s.ActivityName,
                            Balance = s.Balance,
                            BalanceNum = s.BalanceNum,
                            Common = s.Common,
                            CreateTime = s.CreateTime,
                            Name = s.Name,
                            Num = s.Num,
                            Number = s.Number,
                            Remark = s.Remark,
                            State = s.State,
                            Type = s.Type
                        }).ToList();
            }
            return Ok(list);
        }
        [Route("api/SSearch")]
        [HttpGet]
        public IHttpActionResult SSearch()
        {
            List<RedPacketViewModel> list = new List<RedPacketViewModel>();
            using (var ef = new Model1())
            {
                list = (from s in ef.RedState
                        select new RedPacketViewModel
                        {
                            SID = s.SID,
                            SName = s.SName
                        }).ToList();
            }
            return Ok(list);
        }
        [Route("api/TSearch")]
        [HttpGet]
        public IHttpActionResult TSearch()
        {
            List<RedPacketViewModel> list = new List<RedPacketViewModel>();
            using (var ef = new Model1())
            {
                list = (from s in ef.RedType
                        select new RedPacketViewModel
                        {
                            TID = s.TID,
                            TName = s.TName
                        }).ToList();
            }
            return Ok(list);
        }
        //条件查询
        [Route("api/Searchs")]
        [HttpGet]
        public IHttpActionResult Searchs(string name)
        {
            List<RedPacketViewModel> list = new List<RedPacketViewModel>();
            using (var ef = new Model1())
            {
                list = (from s in ef.RedPacket
                        where s.Name.Contains(name)
                        select new RedPacketViewModel
                        {
                            ID = s.ID,
                            ActivityName = s.ActivityName,
                            Balance = s.Balance,
                            BalanceNum = s.BalanceNum,
                            Common = s.Common,
                            CreateTime = s.CreateTime,
                            Name = s.Name,
                            Num = s.Num,
                            Number = s.Number,
                            Remark = s.Remark,
                            State = s.State,
                            Type = s.Type
                        }).ToList();
            }
            return Ok(list);
        }
        //添加
        [Route("api/Insert")]
        [HttpPost]
        public int Insert(RedPacketViewModel m)
        {
            int flag = 0;
            using (var ef = new Model1())
            {
                RedPacket red = new RedPacket
                {
                    ActivityName = m.ActivityName,
                    Balance = m.Balance,
                    BalanceNum = m.BalanceNum,
                    Common = m.Common,
                    CreateTime = DateTime.Now,
                    Name = m.Name,
                    Num = m.Num,
                    Number = m.Number,
                    Remark = m.Remark,
                    State = m.State,
                    Type = m.Type
                };
                ef.Entry(red).State = System.Data.Entity.EntityState.Added;
                flag = ef.SaveChanges();
            }
            return flag;
        }
        //删除
        [Route("api/Del")]
        [HttpPost]
        public int Del(RedPacketViewModel m)
        {
            int falg = 0;
            using (var ef = new Model1())
            {
                var list = (from s in ef.RedPacket where s.ID == m.ID select s).FirstOrDefault();
                ef.Entry(list).State = System.Data.Entity.EntityState.Deleted;
                falg = ef.SaveChanges();
            }
            return falg;
        }
        //修改
        [Route("api/Upt")]
        [HttpPost]
        public int Upt(RedPacketViewModel m)
        {
            int falg = 0;
            using (var ef = new Model1())
            {
                var list = (from s in ef.RedPacket where s.ID == m.ID select s).FirstOrDefault();
                list.Name = m.Name; list.Num = m.Num; list.Number = m.Number;
                list.Remark = m.Remark; list.ActivityName = m.ActivityName; list.Balance = m.Balance;
                list.BalanceNum = m.BalanceNum; list.Common = m.Common;
                list.State = m.State; list.Type = m.Type;
                ef.Entry(list).State = System.Data.Entity.EntityState.Modified;
                falg = ef.SaveChanges();
            }
            return falg;
        }
        //反填
        [Route("api/Fill")]
        [HttpGet]
        public IHttpActionResult Fill(int ID)
        {
            RedPacketViewModel list = new RedPacketViewModel();
            using (var ef = new Model1())
            {
                list = (from s in ef.RedPacket
                        where s.ID == ID
                        select new RedPacketViewModel
                        {
                            ID = s.ID,
                            ActivityName = s.ActivityName,
                            Balance = s.Balance,
                            BalanceNum = s.BalanceNum,
                            Common = s.Common,
                            CreateTime = s.CreateTime,
                            Name = s.Name,
                            Num = s.Num,
                            Number = s.Number,
                            Remark = s.Remark,
                            State = s.State,
                            Type = s.Type
                        }).FirstOrDefault();
            }
            return Ok(list);
        }
    }
}

EF Api 全套

上一篇:VS启动项目 This item does not support previewing


下一篇:转(以作记录):cmd命令行---进行Windows服务操作