.net core mysql 配置_.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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值