栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.net mysql 增删改查

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.net mysql 增删改查

1.创建1个web项目

2.加入Microsoft.EntityFrameworkCore依赖

3.创建数据库表

CREATE TABLE `Person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(20) DEFAULT NULL,
  `Age` int(2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

4.创建1个实体类

namespace mysql1.model
{
    public class Person
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

5.创建mysql配置类

using Microsoft.EntityFrameworkCore;
using mysql1.model;

namespace mysql1.config
{
    public class DBContextHelper : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql("server=xxx;port=xxx;user=xxx;password=xxx;database=xxx", new MySqlServerVersion(new Version(5, 7, 16)));

        }

        public virtual DbSet? Person { get; set; }
    }
}

6.创建1个controller

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using mysql1.config;
using mysql1.model;

namespace mysql1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MyController : ControllerBase
    {
        [HttpGet(Name = "my")]
        public Object Get()
        {
            DBContextHelper helper = new();

            

            

            

            

            return helper.Database.ExecuteSqlRaw("update Person set name='拉拉' where id=29");
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号