引入Pomelo.EntityFrameworkCore.MySql程序包
ConfigureServices
// other service configurations go here
// replace "YourDbContext" with the class name of your DbContext
services.AddDbContextPool<DbModel>(options => options
// replace with your connection string
.UseMySql("Server=localhost;Database=test;User=root;Password=woshishui;", mySqlOptions => mySqlOptions
// replace with your Server Version and Type
.ServerVersion(new ServerVersion(new Version(8, 0, 19), ServerType.MySql))
));
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;database=test;uid=root;pwd=woshishui;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
創建Models實體類
alltestitem
using System.ComponentModel.DataAnnotations;
namespace BackStageCore3.Models
{
public class alltestitem
{
// <summary>
/// 機型
/// </summary>
[Key]
public string 機型 { get; set; }
/// <summary>
/// 測試項目
/// </summary>
public string 測試項目 { get; set; }
/// <summary>
/// 耳機指令
/// </summary>
public string 耳機指令 { get; set; }
/// <summary>
/// 數值上限
/// </summary>
public string 數值上限 { get; set; }
/// <summary>
/// 數值下限
/// </summary>
public string 數值下限 { get; set; }
/// <summary>
/// 編號
/// </summary>
public int 編號 { get; set; }
}
}
Controllers
AlltestitemController.cs
using BackStageCore3.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BackStageCore3.Controllers
{
[ApiController]
[Route("[controller]")]
public class AlltestitemController : ControllerBase
{
private readonly DbModel _coreDbContext;
public AlltestitemController(DbModel coreDbContext)
{
_coreDbContext = coreDbContext;
}
/// <summary>
/// 查詢所有
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult<IEnumerable<alltestitem>>> Get()
{
return await _coreDbContext.Alltestitem.ToListAsync();
}
/// <summary>
/// 查詢type類型下的內容
/// </summary>
/// <param name="id">條件</param>
/// <returns>返回text</returns>
[HttpGet("{id}", Name = "Getgj")]
public List<alltestitem> Get(string id)
{
return _coreDbContext.Set<alltestitem>().Where(b => b.機型 == id).ToList();
}
/// <summary>
/// 添加數據
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
// POST: api/Gj
[HttpPost]
public async Task<ActionResult<alltestitem>> Post(alltestitem gjs)
{
_coreDbContext.Alltestitem.Add(gjs);
await _coreDbContext.SaveChangesAsync();
//CreatedAtAction(actionName,routeValues,value).
return CreatedAtAction(nameof(Get), new { id = gjs.機型 }, gjs);
}
/// <summary>
/// 按條件更新數據
/// </summary>
/// <param name="id"></param>
/// <param name="item"></param>
/// <returns></returns>
// PUT: api/Gj/5
[HttpPut("{id}")]
public async Task<IActionResult> Put(string id, alltestitem item)
{
if (id != item.機型)
{
return BadRequest();
}
_coreDbContext.Entry(item).State = EntityState.Modified;
await _coreDbContext.SaveChangesAsync();
return NoContent();
}
/// <summary>
/// 刪除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// DELETE: api/ApiWithActions/5
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
var todoItem = await _coreDbContext.Alltestitem.FindAsync(id);
if (todoItem == null)
{
return NotFound();
}
_coreDbContext.Alltestitem.Remove(todoItem);
await _coreDbContext.SaveChangesAsync();
return NoContent();
}
}
}
