具有实体框架的CRUD WEB API
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了具有实体框架的CRUD WEB API相关的知识,希望对你有一定的参考价值。
我开始开发我的项目-使用数据库的Web应用程序。我将WEB API与实体框架一起使用我需要在项目中实现CRUD操作。
阅读-工作正常
但是我不知道如何实现创建,更新,删除;我没有足够的经验,很高兴您的建议。
我尝试使用存储库模式和结构模式来实现我的应用程序的良好体系结构。如果您对我的项目的体系结构有任何建议,我将不胜感激。
我不知道如何在价值控制者和资源库中实现它,请您帮忙吗?
附上我的代码:
存储库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebAPI
public class CustomerRepository
public IQueryable<Customer> GetAllCustomers()
DevelopersEntities dev = new DevelopersEntities();
return dev.Customers;
public IQueryable<Customer> GetAllCustomers(int id)
DevelopersEntities dev = new DevelopersEntities();
return dev.Customers.Where(c=>c.Id==id).Select(e=>e);
public IQueryable<Customer> DeleteCustomer(int id)
DevelopersEntities dev = new DevelopersEntities();
return dev.Customers.Remove(id);
public IQueryable<Customer> CreateCustomer()
DevelopersEntities dev = new DevelopersEntities();
public IQueryable<Customer> UpdateCustomer(int id)
DevelopersEntities dev = new DevelopersEntities();
客户模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebAPI;
namespace DevelopersWeb.Models
public class CustomerModel
public int CustomerId get; set;
public string CustomerName get; set;
public IEnumerable<HardwareModel> Hardware get; set;
public IEnumerable<SoftwareModel> Software get; set;
硬件模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DevelopersWeb.Models
public class HardwareModel
public int HardwareId get; set;
public string HardwareName get; set;
软件模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DevelopersWeb.Models
public class SoftwareModel
public int SoftwareId get; set;
public string SoftwareName get; set;
模型工厂
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebAPI;
namespace DevelopersWeb.Models
public class ModelFactory
public CustomerModel Create(Customer customer)
return new CustomerModel()
CustomerId = customer.Id,
CustomerName = customer.Name,
Hardware = customer.HardWares.Select(h=>Create(h)),
Software = customer.Softwares.Select(c=>Create(c))
;
public HardwareModel Create(HardWare hardware)
return new HardwareModel()
HardwareId = hardware.HardWareId,
HardwareName = hardware.HardWareName,
;
public SoftwareModel Create(Software software)
return new SoftwareModel()
SoftwareId = software.SoftwareId,
SoftwareName = software.SoftwareName
;
值控制器
using DevelopersWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPI;
namespace DevelopersWeb.Controllers
public class ValuesController : ApiController
ModelFactory _modelFactory;
public ValuesController()
_modelFactory = new ModelFactory();
// GET api/values
public IEnumerable<CustomerModel> Get()
CustomerRepository cr = new CustomerRepository();
return cr.GetAllCustomers().ToList().Select(c=> _modelFactory.Create(c));
// GET api/values/5
public string Get(int id)
return "xxx";
// POST api/values
public void Post([FromBody]string value)
// PUT api/values/5
public void Put(int id, [FromBody]string value)
// DELETE api/values/5
public void Delete(int id)
答案
这里是保存客户对象的方式:
public void CreateCustomer(Customer customer)
DevelopersEntities dev = new DevelopersEntities();
dev.Customers.Add(customer)
dev.SaveChanges();
这是您的api操作:
// POST api/values
public void Post([FromBody]CustomerModel customerModel)
//Here you should transform CustomerModel object to customerEntity
CustomerRepository cr = new CustomerRepository();
cr.CreateCusomer(customer);
return Ok();
以上是关于具有实体框架的CRUD WEB API的主要内容,如果未能解决你的问题,请参考以下文章