using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace myApi.Controllers
{
[Produces("application/json")]
[Route("api/[controller]/[action]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly mydbContext _context;
public UserController(mydbContext context)
{
this._context = context;
}
[HttpGet]
public IActionResult Login()
{
return Ok("hello");
}
[HttpPost]
public int Add()
{
members mm = new members();
mm.name = "天才";
_context.Set<members>().Add(mm);
_context.SaveChanges();
return 200;
}
}
}
报错如图所示
其他的配置文件如图
数据库文件
这里有个.NET 5/.NET Core使用EF Core 5(Entity Framework Core)连接MySQL数据库写入/读取数据示例教程,希望对你有用。