so I’m making a EFCore API to connect my MAUI app to my SQLServer database.
This is my “tblUser” table’s controller (Select, Create, Update, Delete, all the usual stuff):
UserController.cs:
[Route("api/[controller]")]
[ApiController]
public class tblUserController : ControllerBase
{
private readonly QDbContext _qDbContext;
public tblUserController(QDbContext qDbContext) => _qDbContext = qDbContext;
[HttpGet]
public ActionResult<IEnumerable<tblUser>> Get()
{
return _qDbContext.tblUsers;
}
[HttpGet("{id}")]
public async Task<ActionResult<tblUser?>> GetById(int id)
{
return await _qDbContext.tblUsers.Where(x => x.ID == id).SingleOrDefaultAsync();
}
[HttpPost]
public async Task<ActionResult> Create(tblUser user)
{
await _qDbContext.tblUsers.AddAsync(user);
await _qDbContext.SaveChangesAsync();
return CreatedAtAction(nameof(GetById), new { id = user.ID }, user);
}
[HttpPut]
public async Task<ActionResult> Update(tblUser user)
{
_qDbContext.tblUsers.Update(user);
await _qDbContext.SaveChangesAsync();
return Ok();
}
[HttpDelete("{id}")]
public async Task<ActionResult> Delete(int id)
{
var userGetByIdResult = await GetById(id);
if (userGetByIdResult.Value is null)
return NotFound();
_qDbContext.Remove(userGetByIdResult.Value);
await _qDbContext.SaveChangesAsync();
return Ok();
}
}
I really didn’t want to repeat the same thing for each table, so I made a parent class that gets the type as “TEntity” and does the same stuff:
DbController.cs
[Route("api/[controller]")]
[ApiController]
public class DbController<TEntity> : ControllerBase where TEntity : class
{
private readonly DbContext _context;
private readonly DbSet<TEntity> _dbSet;
public DbController(DbContext context)
{
_context = context;
_dbSet = _context.Set<TEntity>();
}
[HttpGet]
public async Task<ActionResult<IEnumerable<TEntity>>> Get()
{
return await _dbSet.ToListAsync();
}
[HttpGet("{id}")]
public async Task<ActionResult<TEntity>> GetById(int id)
{
TEntity? entity = await _dbSet.FindAsync(id);
return entity == null ? (ActionResult<TEntity>)NotFound() : (ActionResult<TEntity>)entity;
}
[HttpPost]
public async Task<ActionResult> Create(TEntity entity)
{
_ = await _dbSet.AddAsync(entity);
_ = await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetById), new { id = entity.GetType().GetProperty("ID")?.GetValue(entity) }, entity);
}
[HttpPut("{id}")]
public async Task<ActionResult> Update(int id, TEntity entity)
{
TEntity? existingEntity = await _dbSet.FindAsync(id);
if (existingEntity == null)
{
return NotFound();
}
_context.Entry(existingEntity).CurrentValues.SetValues(entity);
_ = await _context.SaveChangesAsync();
return Ok();
}
[HttpDelete("{id}")]
public async Task<ActionResult> Delete(int id)
{
TEntity? entity = await _dbSet.FindAsync(id);
if (entity == null)
{
return NotFound();
}
_ = _dbSet.Remove(entity);
_ = await _context.SaveChangesAsync();
return Ok();
}
}
and inherited my User controller from it:
UserController.cs
[Route("api/[controller]")]
[ApiController]
public class tblUsersController : DbController<tblUser>
{
public tblUsersController(QDbContext context) : base(context)
{
}
}
The problem is that when I use GetItems() I get a 404 error although swagger works fine!
also when I roll back my parent class making, GetItems() works.