Net8 EFCore Mysql 连接

这篇具有很好参考价值的文章主要介绍了Net8 EFCore Mysql 连接。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、安装插件

  • Pomelo.EntityFrameworkCore.MySq (这里要选8.0.0以上版本低版本不支持.net8)

二、配置数据库连接串

 appsettings.json 中配置数据库连接串

"ConnectionStrings": {
  "Connection": "server=172.18.2.183;port=3306;database=students;uid=root;pwd=123456;CharSet=utf8"
}

三、添加实体类Student和数据库上下文

新建 Entities 目录,在,根据表及字段,在目录下新建 Student 实体类,在类上加  [Table("student")] 表名、属性上加[Column("id")] 字段名等与表对应,代码如下:

using System.ComponentModel.DataAnnotations.Schema;

namespace Snai.Mysql.Entities
{
    [Table("students")]
    public class Student
    {
        [Column("id")]
        public int ID { get; set; }

        [Column("name")]
        public string Name { get; set; }
    }
}

在根目录下加上 DataAccess 目录做为数据库操作目录,在该目录下加上 Base 目录做数据库上下文目录

在 Base 目录下新建 SqlContext 上下文类,继承 DbContext 类,通过构造函数注入数据库连接,添加 DbSet<Student> 实体属性,代码如下:

using Microsoft.EntityFrameworkCore;
using Snai.Mysql.Entities;

namespace Server.DataAccess.Base
{
    public class SqlContext : DbContext
    {
        public SqlContext(DbContextOptions<SqlContext> options)
            : base(options)
        { }

        public DbSet<Student> Student { get; set; }
    }
}

在DataAccess目录下创建Interface和Implement文件夹分别为数据库对应数据的接口和实现。

接口定义:

using Server.Mysql.Entities;

namespace Server.DataAccess.Interface
{
    public interface IStudentDao
    {
        //插入数据
        bool CreateStudent(Student student);

        //取全部记录
        IEnumerable<Student> GetStudents();

        //取某id记录
        Student GetStudentByID(int id);

        //根据id更新整条记录
        bool UpdateStudent(Student student);

        //根据id更新名称
        bool UpdateNameByID(int id, string name);

        //根据id删掉记录
        bool DeleteStudentByID(int id);
    }
}

实现:

using Server.DataAccess.Base;
using Server.DataAccess.Interface;
using Server.Mysql.Entities;

namespace Server.DataAccess.Implement
{
    public class StudentDao : IStudentDao
    {
        private SqlContext _context;

        public StudentDao(SqlContext context)
        {
            _context = context;
        }

        //插入数据
        public bool CreateStudent(Student student)
        {
            _context.Student.Add(student);
            return _context.SaveChanges() > 0;
        }

        //取全部记录
        public IEnumerable<Student> GetStudents()
        {
            return _context.Student.ToList();
        }

        //取某id记录
        public Student? GetStudentByID(int id)
        {
            return _context.Student.SingleOrDefault(s => s.ID == id);
        }

        //根据id更新整条记录
        public bool UpdateStudent(Student student)
        {
            _context.Student.Update(student);
            return _context.SaveChanges() > 0;
        }

        //根据id更新名称
        public bool UpdateNameByID(int id, string name)
        {
            var state = false;
            var student = _context.Student.SingleOrDefault(s => s.ID == id);

            if (student != null)
            {
                student.Name = name;
                state = _context.SaveChanges() > 0;
            }

            return state;
        }

        //根据id删掉记录
        public bool DeleteStudentByID(int id)
        {
            var student = _context.Student.SingleOrDefault(s => s.ID == id);
            _context.Student.Remove(student);
            return _context.SaveChanges() > 0;
        }
    }
}

依赖注入

在Program.cs中写入

string? sqlConnection = builder.Configuration.GetConnectionString("Connection");
if (sqlConnection != null) 
{
    builder.Services.AddDbContext<SqlContext>(options =>
    {
        var serverVersion = ServerVersion.AutoDetect(sqlConnection);  //mysql版本: {8.2.0-mysql}
        options.UseMySql(sqlConnection, serverVersion);
    });
}
builder.Services.AddScoped<IStudentDao, StudentDao>(); //对于同一个请求返回同一个实例

设计表

.net core 8 使用efcore,net8,mysql,数据库

Controller调用

private IStudentDao _iStudentDao;

public TestController(IStudentDao iStudentDao)  //构造函数中添加
{
    _iStudentDao = iStudentDao;
}

//调用测试方法创建
public void TestCreate()
{
    Student student = new Student();
    student.ID = 1;
    student.Name = "在下没有钱";
    _iStudentDao.CreateStudent(student);
}

运行结果:

.net core 8 使用efcore,net8,mysql,数据库文章来源地址https://www.toymoban.com/news/detail-834822.html

到了这里,关于Net8 EFCore Mysql 连接的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ASP.Net Core Web Api+EFCore+MySql实现动态查询(保姆教学)

    本文会详细讲解如何从打开文件到第一个API开发完成,过程十分详细,是基于学习入门。 现在让我们开始吧! 打开VS(演示用的Visual Studio2022) 第一步我们选择创建新项目   第二步 选择开发语言以及应用程序 我们选择C# -所有平台-Web API.找到 ASP.NET Core Web API 应用   这里应用

    2024年02月12日
    浏览(28)
  • .NET6 + EF Core + MySQL 创建实体和数据库、EFCore 数据迁移

    接上期文章《.NET6项目连接数据库方式方法》,有人问了我几个问题,现在就这几个问题,拓展延申一下创建实体类、数据库。把ORM框架和数据迁移都写进去。 我的项目是在Linux上创建的,使用的是vscode开发工具远程开发。为了方便大家阅读和操作,我将项目down到我的本地电

    2024年02月05日
    浏览(35)
  • .net 连接MySql数据库 + 使用Microsoft.EntityFrameworkCore.Design自动生成实体类 + 使用EFCore操作数据库

    先准备好一个mysql数据库(我这里准备的是test数据库,里面又准备了两张表,其中book表中只有两个字段,Id(bigint类型)和 Name(varchar类型)) 使用VS新建一个asp.net core web api项目(我这里使用的框架是.net5.0的,确保版本对应很重要) 打开终端 进到项目所在目录(我这里解

    2024年02月07日
    浏览(30)
  • asp.net core EFCore 属性配置与DbContext

    Entity Framework (EF) Core 是轻量化、可扩展、开源和跨平台版的常用 Entity Framework 数据访问技术。用于程序中的class类和数据库中的表互相之间建立映射关系。在学习过程中,EFCore中的属性配置显的尤为重要它是学习好asp.net core的基础是配置数据库表结构的重要基石。本篇内容为

    2024年02月07日
    浏览(34)
  • ASP.NET Core 3.1系列(16)——EFCore之Code First

    前一篇博客介绍了 EFCore 中的 DB First 开发模式,该模式可以根据数据库生成实体类和数据库上下文,因此适用于数据库已经存在的场景。而与之相对应的, Code First 主要是根据自定义的实体类和数据库上下文反向构建数据库,因此也可以看做是 DB First 的逆过程,下面开始介绍

    2024年01月17日
    浏览(43)
  • ASP.NET Core 3.1系列(15)——EFCore之DB First

    本文开始介绍一些关于 Entity Framework Core 的内容。在 EFCore 中,常用的为 DB First 模式和 Code First 模式,下面就来介绍一下如何在 EFCore 中使用 DB First 模式生成实体类和数据库上下文。 在 SQL Server 中新建一个数据库 Dao ,执行如下语句,创建 Country 和 Province 数据表。 运行结果如

    2024年02月15日
    浏览(31)
  • asp.net core 6.0 efcore +sqlserver增删改查的demo

    下面是一个使用ASP.NET Core 5.0和Entity Framework Core进行增删改查操作的示例。 首先,创建一个空的ASP.NET Core 6.0 Web应用程序项目。 然后,安装以下NuGet包: Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools 接下来,创建一个数据库上下文类,用于定义实体类和数据库连接

    2024年02月13日
    浏览(30)
  • abp(net core)+easyui+efcore实现仓储管理系统——模块管理升级(六十)

    abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+easyui+efcore实现仓储管理系统——解决方案介绍(二) abp(net core)+easyui+efcore实现仓储管理系统——领域层创建实体(三)   abp(net core)+easyui+efcore实现仓储管理系统——定义仓储并实现 (四) abp(net core)+easyui+efcore实

    2023年04月09日
    浏览(37)
  • 在NET8中使用简化的 AddJwtBearer 认证

    系统版本: win10 .NET SDK: NET8 开发工具:vscode 参考引用:使用 dotnet user-jwts 管理开发中的 JSON Web 令牌 注意:以下示例中的端口、token等需替换成你的环境中的信息 运行以下命令来创建一个空的 Web 项目,并添加 Microsoft.AspNetCore.Authentication.JwtBearer NuGet 包: 将 Program.cs 的内容替

    2024年02月05日
    浏览(29)
  • abp(net core)+easyui+efcore实现仓储管理系统——组织管理升级之下(六十二)

    A bp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+easyui+efcore实现仓储管理系统——解决方案介绍(二) abp(net core)+easyui+efcore实现仓储管理系统——领域层创建实体(三)   abp(net core)+easyui+efcore实现仓储管理系统——

    2023年04月23日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包