删除数据
[HttpDelete]
public ResultModel DeleteDataById(int id)
{
var result = new ResultModel();
//实例化数据库访问上下文
using (var ef = new CrmEntity())
{
//先查后删
var model = ef.data.FirstOrDefault(d => d.id == id);
if (model != null)
{
ef.data.Remove(model);
if (ef.SaveChanges() > 0)
{
result.code = 200;
result.message = "删除成功!";
}
else
{
result.code = 500;
result.message = "网络问题,请刷新后重试";
}
}
else
{
result.code = 404;
//如果不存在则提示错误消息
result.message = "当前的学员已经删除!";
}
return result;
}
}
注:ResultModel类见WebApi_Insert文章,MVC API +EntityFramework技术实现该功能;