一、安裝插件
- Pomelo.EntityFrameworkCore.MySq (這里要選8.0.0以上版本低版本不支持.net8)
二、配置數(shù)據(jù)庫連接串
?appsettings.json 中配置數(shù)據(jù)庫連接串
"ConnectionStrings": {
"Connection": "server=172.18.2.183;port=3306;database=students;uid=root;pwd=123456;CharSet=utf8"
}
三、添加實(shí)體類Student和數(shù)據(jù)庫上下文
新建 Entities 目錄,在,根據(jù)表及字段,在目錄下新建 Student 實(shí)體類,在類上加 ?[Table("student")] 表名、屬性上加[Column("id")] 字段名等與表對(duì)應(yīng),代碼如下:
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 目錄做為數(shù)據(jù)庫操作目錄,在該目錄下加上 Base 目錄做數(shù)據(jù)庫上下文目錄
在 Base 目錄下新建 SqlContext 上下文類,繼承 DbContext 類,通過構(gòu)造函數(shù)注入數(shù)據(jù)庫連接,添加 DbSet<Student> 實(shí)體屬性,代碼如下:
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目錄下創(chuàng)建Interface和Implement文件夾分別為數(shù)據(jù)庫對(duì)應(yīng)數(shù)據(jù)的接口和實(shí)現(xiàn)。
接口定義:
using Server.Mysql.Entities;
namespace Server.DataAccess.Interface
{
public interface IStudentDao
{
//插入數(shù)據(jù)
bool CreateStudent(Student student);
//取全部記錄
IEnumerable<Student> GetStudents();
//取某id記錄
Student GetStudentByID(int id);
//根據(jù)id更新整條記錄
bool UpdateStudent(Student student);
//根據(jù)id更新名稱
bool UpdateNameByID(int id, string name);
//根據(jù)id刪掉記錄
bool DeleteStudentByID(int id);
}
}
實(shí)現(xiàn):
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;
}
//插入數(shù)據(jù)
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);
}
//根據(jù)id更新整條記錄
public bool UpdateStudent(Student student)
{
_context.Student.Update(student);
return _context.SaveChanges() > 0;
}
//根據(jù)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;
}
//根據(jù)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>(); //對(duì)于同一個(gè)請(qǐng)求返回同一個(gè)實(shí)例
設(shè)計(jì)表
Controller調(diào)用
private IStudentDao _iStudentDao;
public TestController(IStudentDao iStudentDao) //構(gòu)造函數(shù)中添加
{
_iStudentDao = iStudentDao;
}
//調(diào)用測(cè)試方法創(chuàng)建
public void TestCreate()
{
Student student = new Student();
student.ID = 1;
student.Name = "在下沒有錢";
_iStudentDao.CreateStudent(student);
}
運(yùn)行結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-834822.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-834822.html
到了這里,關(guān)于Net8 EFCore Mysql 連接的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!