.Net Core3.1 配置Pomelo.EntityFrameworkCore.MySql数据操作(2)

引入Pomelo.EntityFrameworkCore.MySql程序包

ConfigureServices

// other service configurations go here
            // replace "YourDbContext" with the class name of your DbContext
            services.AddDbContextPool(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
    {
        // 
        /// 机型
        /// 
        [Key]
        public string 机型 { get; set; }

        /// 
        /// 测试项目
        /// 
        public string 测试项目 { get; set; }
        /// 
        /// 耳机指令
        /// 
        public string 耳机指令 { get; set; }
        /// 
        /// 数值上限
        /// 
        public string 数值上限 { get; set; }
        /// 
        /// 数值下限
        /// 
        public string 数值下限 { get; set; }
        /// 
        /// 编号
        /// 
        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;
        }


        /// 
        /// 查询所有
        /// 
        /// 
        [HttpGet]
        public async Task>> Get()
        {
            return await _coreDbContext.Alltestitem.ToListAsync();
        }

        /// 
        /// 查询type类型下的内容
        /// 
        /// 条件
        /// 返回text
        [HttpGet("{id}", Name = "Getgj")]
        public List Get(string id)
        {
            return _coreDbContext.Set().Where(b => b.机型 == id).ToList();
        }


        /// 
        /// 添加数据
        /// 
        /// 
        /// 
        // POST: api/Gj
        [HttpPost]
        public async Task> Post(alltestitem gjs)
        {
            _coreDbContext.Alltestitem.Add(gjs);
            await _coreDbContext.SaveChangesAsync();
            //CreatedAtAction(actionName,routeValues,value).
            return CreatedAtAction(nameof(Get), new { id = gjs.机型 }, gjs);
        }


        /// 
        /// 按条件更新数据
        /// 
        /// 
        /// 
        /// 
        // PUT: api/Gj/5
        [HttpPut("{id}")]
        public async Task Put(string id, alltestitem item)
        {
            if (id != item.机型)
            {
                return BadRequest();
            }
            _coreDbContext.Entry(item).State = EntityState.Modified;
            await _coreDbContext.SaveChangesAsync();

            return NoContent();
        }
        /// 
        /// 删除
        /// 
        /// 
        /// 
        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public async Task Delete(int id)
        {
            var todoItem = await _coreDbContext.Alltestitem.FindAsync(id);
            if (todoItem == null)
            {
                return NotFound();
            }
            _coreDbContext.Alltestitem.Remove(todoItem);
            await _coreDbContext.SaveChangesAsync();
            return NoContent();
        }
    }
}

你可能感兴趣的:(.Net Core3.1 配置Pomelo.EntityFrameworkCore.MySql数据操作(2))