我必须在不使用实体框架的情况下使用USIG Web API创建Crud函数. [英] I have to create crud functions usig web api without using an entity framework .

查看:91
本文介绍了我必须在不使用实体框架的情况下使用USIG Web API创建Crud函数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用实体框架使用webApi和MVC创建了crud函数,但是,我必须做同样的事情,而无需使用我无法做到的实体框架.我是这里的初学者,有人可以帮助我吗?我真的需要修复

我尝试过的事情:

我已经创建了一个名为Abc,Repository和Api contoller(用于Mvc的控制器)的Model类,我已经将"Abc"表用于实体框架和图表.

存储库中的类

I have created crud functions using webApi and MVC using an Entity framework, but, I have to do the same thing without using an entity framework which i am not able to do it. I am beginner here, can anybody help me with that? I really need to fix that

What I have tried:

I have created Model class called Abc, Repository, and Api contoller, Controller for Mvc, I have used "Abc" table for Entity framework and diagram.

Class in Repository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebserviceDemo.Models;
using WebserviceDemo.Interface;

namespace WebserviceDemo.Repositories
{
    public class AbcRepository : IAbcRepository
    {
        demoradiologyEntities ProductDB = new demoradiologyEntities();

        public IEnumerable<abc> GetAll()
        {
            // TO DO : Code to get the list of all the records in database
            return ProductDB.Abcs;
        }

        public Abc Get(int id)
        {
            // TO DO : Code to find a record in database
            return ProductDB.Abcs.Find(id);
        }

        public Abc Add(Abc item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            // TO DO : Code to save record into database
            ProductDB.Abcs.Add(item);
            ProductDB.SaveChanges();
            return item;
        }

        public bool Update(Abc item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            // TO DO : Code to update record into database
            var products = ProductDB.Abcs.Single(a => a.ID == item.ID);
            products.Abr = item.Abr;
            products.Name = item.Name;
            ProductDB.SaveChanges();

            return true;
        }

        public bool Delete(int id)
        {
            // TO DO : Code to remove the records from database
            Abc products = ProductDB.Abcs.Find(id);
            ProductDB.Abcs.Remove(products);
            ProductDB.SaveChanges();
            return true;
        }
    }
}



界面:



Interface:

using WebserviceDemo.Models;
namespace WebserviceDemo.Interface
{
    interface IAbcRepository
    {

        IEnumerable<abc> GetAll();
        Abc Get(int id);
        Abc Add(Abc item);
        bool Update(Abc item);
        bool Delete(int id);
    }
}


api控制器


Api controller

namespace WebserviceDemo.Controllers
{
    public class AbcController : ApiController
    {
        static readonly IAbcRepository repository = new AbcRepository();

        public IEnumerable<abc> GetAllAbc()
        {
            return repository.GetAll();
        }

        public Abc PostAbc(Abc item)
        {
            return repository.Add(item);
        }

        public IEnumerable<abc> PutAbc(int id, Abc product)
        {
            product.ID = id;
            if (repository.Update(product))
            {
                return repository.GetAll();
            }
            else
            {
                return null;
            }
        }

        public bool DeleteAbc(int id)
        {
            if (repository.Delete(id))
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
}


控制器


Controller

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebserviceDemo.Models;
using WebserviceDemo.Interface;
using WebserviceDemo.Repositories;

namespace WebserviceDemo.Controllers
{
    public class BcdController : Controller
    {
        public  demoradiologyEntities db = new demoradiologyEntities();

        // GET: /Bcd/
        public ActionResult Index()
        {
            return View(db.Abcs.ToList());
        }

        // GET: /Bcd/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Abc abc = db.Abcs.Find(id);
            if (abc == null)
            {
                return HttpNotFound();
            }
            return View(abc);
        }

        // GET: /Bcd/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Bcd/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="ID,Abr,Name")] Abc abc)
        {
            if (ModelState.IsValid)
            {
                db.Abcs.Add(abc);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(abc);
        }

        // GET: /Bcd/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Abc abc = db.Abcs.Find(id);
            if (abc == null)
            {
                return HttpNotFound();
            }
            return View(abc);
        }

        // POST: /Bcd/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,Abr,Name")] Abc abc)
        {
            if (ModelState.IsValid)
            {
                db.Entry(abc).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(abc);
        }

        // GET: /Bcd/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Abc abc = db.Abcs.Find(id);
            if (abc == null)
            {
                return HttpNotFound();
            }
            return View(abc);
        }

        // POST: /Bcd/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Abc abc = db.Abcs.Find(id);
            db.Abcs.Remove(abc);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

推荐答案

您可以使用DbCommandBuilder:
You could use DbCommandBuilder:
using (DbCommand cmd = conn.CreateCommand())
{
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from " + tableName;
    cmd.CommandTimeout = 10;
    using (DbDataAdapter da = factory.CreateDataAdapter())
    {
        DbCommandBuilder cb = factory.CreateCommandBuilder();
        da.SelectCommand = cmd;
        DataTable dt = new DataTable();
        da.FillSchema(dt, SchemaType.Source);
        cb.DataAdapter = da;
        DbCommand[] cmds = new DbCommand[3]
        cmds[0] = cb.GetUpdateCommand();
        cmds[1] = cb.GetDeleteCommand();
        cmds[2] = cb.GetInsertCommand();
    }
}


有关更多信息,请参见CodeProject上的这篇文章:不要对您的DataProviders进行硬编码 [ ^ ]


See this article on CodeProject for more information: Don''t hard code your DataProviders[^]


这篇关于我必须在不使用实体框架的情况下使用USIG Web API创建Crud函数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆