CustomerController.cs using System.Data.Entity; using Syste

时间:2021-12-04 05:23:54

比来做了一个项目技术预研:在ASP.NET MVC框架中使用Web API和EntityFramework,构建一个根本的架构,并在此根本上实现根基的CRUD应用。

以下是详细的法式。

第一步

在数据库中创建一张数据表,表名为Customer,见下图:

第二步

打开 Visual Studio,新建项目。

选择‘ASP.NET Web Application‘,定名为‘WebApi‘。

我们要创建一个‘Web API‘,在模板中选择‘Web API‘,点击‘确定‘。

第三步

接下来我要添加一个类。

右键点击这个 web api项目,添加一个‘ADO.NET Entity Data Model‘。

添加‘EF Designer from database‘,点击‘下一步‘。

在配置窗口中添加新连接,输入处事器名称,选择数据库,点击‘确定‘。

点击‘下一步‘,选择要关联的数据表。

点击‘完成‘。

第四步

此刻的事情就是要为Wep Api添加控制器了。

右键选择‘controllers‘文件夹。

选择‘MVC5 Controller with views, using Entity Framework‘。

点击‘添加‘。

选择数据模型类Customer,选择Data context类文件,本例中为SystemTestEntity。

设置控制器名称为CustomersController,点击‘添加‘。

上述操纵完成后,CustomerController.cs会自动生成。

CustomerController.cs

using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Net; using System.Web.Http; using System.Web.Http.Description; using CRUDWebApiExam.Models; namespace CRUDWebApiExam.Controllers { public class CustomersController : ApiController { private SystemTestEntities db = new SystemTestEntities(); // GET: api/Customers public IQueryable<Customer> GetCustomer() { return db.Customer; } // GET: api/Customers/5 [ResponseType(typeof(Customer))] public IHttpActionResult GetCustomer(int id) { Customer customer = db.Customer.Find(id); if (customer == null) { return NotFound(); } return Ok(customer); } // PUT: api/Customers/5 [ResponseType(typeof(void))] public IHttpActionResult PutCustomer(int id, Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != customer.CustomerId) { return BadRequest(); } db.Entry(customer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); } // POST: api/Customers [ResponseType(typeof(Customer))] public IHttpActionResult PostCustomer(Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Customer.Add(customer); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer); } // DELETE: api/Customers/5 [ResponseType(typeof(Customer))] public IHttpActionResult DeleteCustomer(int id) { Customer customer = db.Customer.Find(id); if (customer == null) { return NotFound(); } db.Customer.Remove(customer); db.SaveChanges(); return Ok(customer); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } private bool CustomerExists(int id) { return db.Customer.Count(e => e.CustomerId == id) > 0; } } }

运行这个 Web Api项目,你会看到下面的功效

第五步

到此为止,web api已经构建告成了。接下来我们需要为这个web api处事添加消费东西了,这个消费东西就是本文要涉及的MVC项目。

首先要添加一个model类,我们可以直接给与上文中的Customer类。右键点击models文件夹,添加类并定名。本例中,我直接定名为Customer.cs。
Customer.cs

using System; using System.ComponentModel.DataAnnotations; namespace MVCPersatantion.Models { public class Customer { [Display(Name = "CustomerId")] public int CustomerId { get; set; } [Display(Name = "Name")] public string Name { get; set; } [Display(Name = "Address")] public string Address { get; set; } [Display(Name = "MobileNo")] public string MobileNo { get; set; } [Display(Name = "Birthdate")] [DataType(DataType.Date)] public DateTime Birthdate { get; set; } [Display(Name = "EmailId")] public string EmailId { get; set; } } }