我正在尝试学习一些Web api部分。因此创建了一个EF,然后使用数据库上下文创建了一个控制器。这是为我生成的控制器.net。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SanApi.Models;
namespace SanApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TblDepartmentsController : ControllerBase
{
private readonly ClassooContext _context;
public TblDepartmentsController(ClassooContext context)
{
_context = context;
}
// GET: api/TblDepartments
[HttpGet]
public async Task<ActionResult<IEnumerable<TblDepartmentMaster>>> GetTblDepartmentMaster()
{
return await _context.TblDepartmentMaster.ToListAsync();
}
// GET: api/TblDepartments/5
[HttpGet("{id}")]
public async Task<ActionResult<TblDepartmentMaster>> GetTblDepartmentMaster(int id)
{
var tblDepartmentMaster = await _context.TblDepartmentMaster.FindAsync(id);
if (tblDepartmentMaster == null)
{
return NotFound();
}
return tblDepartmentMaster;
}
// PUT: api/TblDepartments/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPut("{id}")]
public async Task<IActionResult> PutTblDepartmentMaster(int id, TblDepartmentMaster tblDepartmentMaster)
{
if (id != tblDepartmentMaster.DepartmentId)
{
return BadRequest();
}
_context.Entry(tblDepartmentMaster).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TblDepartmentMasterExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/TblDepartments
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPost]
public async Task<ActionResult<TblDepartmentMaster>> PostTblDepartmentMaster(TblDepartmentMaster tblDepartmentMaster)
{
_context.TblDepartmentMaster.Add(tblDepartmentMaster);
await _context.SaveChangesAsync();
return CreatedAtAction("GetTblDepartmentMaster", new { id = tblDepartmentMaster.DepartmentId }, tblDepartmentMaster);
}
// DELETE: api/TblDepartments/5
[HttpDelete("{id}")]
public async Task<ActionResult<TblDepartmentMaster>> DeleteTblDepartmentMaster(int id)
{
var tblDepartmentMaster = await _context.TblDepartmentMaster.FindAsync(id);
if (tblDepartmentMaster == null)
{
return NotFound();
}
_context.TblDepartmentMaster.Remove(tblDepartmentMaster);
await _context.SaveChangesAsync();
return tblDepartmentMaster;
}
private bool TblDepartmentMasterExists(int id)
{
return _context.TblDepartmentMaster.Any(e => e.DepartmentId == id);
}
}
}
但它会触发错误提示
nvalidOperationException:无法解析类型的服务尝试激活时出现“ SanApi.Models.ClassooContext”'SanApi.Controllers.TblDepartmentsController'。
自从它自动生成以来,我还没有一个想法,错误在哪里。我是Web API的新手。所以被卡住了。
创建数据库上下文后,如:
public class ClassooContext : DbContext
{
public ClassooContext(DbContextOptions<SchoolContext> options) : base(options)
{
}
public DbSet<Course> Courses { get; set; }
}
您需要注册ClassooContext
。默认情况下,ASP.NET Core实现依赖项注入。服务(例如EF数据库上下文)在应用程序启动期间通过依赖项注入进行注册。要将ClassooContext
注册为服务,请打开Startup.cs
,然后将突出显示的行添加到ConfigureServices
方法中:
services.AddDbContext<ClassooContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
并打开appsettings.jso
n文件并添加一个连接字符串,如:
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
},
然后您可以注入控制器以使用EF上下文,如:
private readonly ClassooContext _context;
public TblDepartmentsController(ClassooContext context)
{
_context = context;
}