You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
then, $ dotnet watch run will rebuild the app on every save.
Create Models directory and Model class:
$ mkdir Models && touch Models/Value.cs
In Value.cs:
public class Value
{
public int Id { get; set; }
public string Name { get; set; }
}
Create DataContext:
mkdir Data && touch Data/DataContext.cs
In DataContext.cs:
using DatingApp.api.Models;
using Microsoft.EntityFrameworkCore;
namespace DatingApp.api.Data
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options) {}
public DbSet<Value> Values { get; set; }
}
}
In Startup.cs:
using DatingApp.api.Data;
using Microsoft.EntityFrameworkCore;
[...]
services.AddDbContext<DataContext>(x => x.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
$ dotnet ef migrations add InitialCreate
$ dotnet ef database update
Retrieving Data
Values Controller (Right now, just the GET and GET/ID endpoints are fleshed out:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DatingApp.api.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace DatingApp.api.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly DataContext _context;
public ValuesController(DataContext context) => _context = context;
// GET api/values
[HttpGet]
public async Task<IActionResult> GetValues()
{
var values = await _context.Values.ToListAsync();
return Ok(values);
}
// GET api/values/5
[HttpGet("{id}")]
public async Task<IActionResult> GetValue(int id)
{
var value = await _context.Values.FirstOrDefaultAsync(x => x.Id == id);
return Ok(value);
}
// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}