对于原始海报来说,这个答案可能为时已晚,但也许其他人可以使用它。
我刚刚为 MVC 5 代码优先 CRUD 操作制作了这个通用的基本 api 控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using My.Models;
using System.Data.Entity.Validation;
namespace My.Controllers.Api
{
public abstract class CrudController<TEntity>
: ApiController where TEntity : class
{
private readonly MyContext _db = new MyContext();
private readonly DbSet<TEntity> _dbSet;
protected CrudController(Func<MyContext, DbSet<TEntity>> dbSet)
{
_db = new EtlContext();
_dbSet = dbSet(_db);
}
public IEnumerable<TEntity> Get()
{
return _dbSet.AsEnumerable();
}
public HttpResponseMessage Post(TEntity entity)
{
try
{
if (!ModelState.IsValid)
return Request.CreateResponse(HttpStatusCode.BadRequest);
_db.Entry(entity).State = EntityState.Added;
_db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.Created);
}
catch (DbEntityValidationException)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
catch (DbUpdateException)
{
return Request.CreateResponse(HttpStatusCode.Conflict);
}
}
public HttpResponseMessage Put(TEntity entity)
{
try
{
if (!ModelState.IsValid)
return Request.CreateResponse(HttpStatusCode.BadRequest);
_db.Entry(entity).State = EntityState.Modified;
_db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (DbEntityValidationException)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
catch (DbUpdateException)
{
return Request.CreateResponse(HttpStatusCode.Conflict);
}
}
public HttpResponseMessage Delete(TEntity entity)
{
try
{
_db.Entry(entity).State = EntityState.Deleted;
_db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
}
protected override void Dispose(bool disposing)
{
_db.Dispose();
base.Dispose(disposing);
}
}
}
我仍然必须像这样为每个 DbSet 创建子类:
public class CustomersController
: CrudController<Customer>
{
public CustomersController()
: base(db => db.Customers)
{}
}
public class ProductsController
: CrudController<Product>
{
public ProductsController()
: base(db => db.Products)
{}
}
使此路由工作:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}",
);