国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Net8 EFCore Mysql 連接

這篇具有很好參考價(jià)值的文章主要介紹了Net8 EFCore Mysql 連接。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、安裝插件

  • 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ì)表

.net core 8 使用efcore,net8,mysql,數(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é)果:

.net core 8 使用efcore,net8,mysql,數(shù)據(jù)庫文章來源地址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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ASP.Net Core Web Api+EFCore+MySql實(shí)現(xiàn)動(dòng)態(tài)查詢(保姆教學(xué))

    ASP.Net Core Web Api+EFCore+MySql實(shí)現(xiàn)動(dòng)態(tài)查詢(保姆教學(xué))

    本文會(huì)詳細(xì)講解如何從打開文件到第一個(gè)API開發(fā)完成,過程十分詳細(xì),是基于學(xué)習(xí)入門。 現(xiàn)在讓我們開始吧! 打開VS(演示用的Visual Studio2022) 第一步我們選擇創(chuàng)建新項(xiàng)目? ?第二步 選擇開發(fā)語言以及應(yīng)用程序 我們選擇C# -所有平臺(tái)-Web?API.找到 ASP.NET Core Web API 應(yīng)用 ? 這里應(yīng)用

    2024年02月12日
    瀏覽(18)
  • .NET6 + EF Core + MySQL 創(chuàng)建實(shí)體和數(shù)據(jù)庫、EFCore 數(shù)據(jù)遷移

    .NET6 + EF Core + MySQL 創(chuàng)建實(shí)體和數(shù)據(jù)庫、EFCore 數(shù)據(jù)遷移

    接上期文章《.NET6項(xiàng)目連接數(shù)據(jù)庫方式方法》,有人問了我?guī)讉€(gè)問題,現(xiàn)在就這幾個(gè)問題,拓展延申一下創(chuàng)建實(shí)體類、數(shù)據(jù)庫。把ORM框架和數(shù)據(jù)遷移都寫進(jìn)去。 我的項(xiàng)目是在Linux上創(chuàng)建的,使用的是vscode開發(fā)工具遠(yuǎn)程開發(fā)。為了方便大家閱讀和操作,我將項(xiàng)目down到我的本地電

    2024年02月05日
    瀏覽(23)
  • .net 連接MySql數(shù)據(jù)庫 + 使用Microsoft.EntityFrameworkCore.Design自動(dòng)生成實(shí)體類 + 使用EFCore操作數(shù)據(jù)庫

    .net 連接MySql數(shù)據(jù)庫 + 使用Microsoft.EntityFrameworkCore.Design自動(dòng)生成實(shí)體類 + 使用EFCore操作數(shù)據(jù)庫

    先準(zhǔn)備好一個(gè)mysql數(shù)據(jù)庫(我這里準(zhǔn)備的是test數(shù)據(jù)庫,里面又準(zhǔn)備了兩張表,其中book表中只有兩個(gè)字段,Id(bigint類型)和 Name(varchar類型)) 使用VS新建一個(gè)asp.net core web api項(xiàng)目(我這里使用的框架是.net5.0的,確保版本對(duì)應(yīng)很重要) 打開終端 進(jìn)到項(xiàng)目所在目錄(我這里解

    2024年02月07日
    瀏覽(32)
  • asp.net core EFCore 屬性配置與DbContext

    Entity Framework (EF) Core 是輕量化、可擴(kuò)展、開源和跨平臺(tái)版的常用 Entity Framework 數(shù)據(jù)訪問技術(shù)。用于程序中的class類和數(shù)據(jù)庫中的表互相之間建立映射關(guān)系。在學(xué)習(xí)過程中,EFCore中的屬性配置顯的尤為重要它是學(xué)習(xí)好asp.net core的基礎(chǔ)是配置數(shù)據(jù)庫表結(jié)構(gòu)的重要基石。本篇內(nèi)容為

    2024年02月07日
    瀏覽(94)
  • ASP.NET Core 3.1系列(16)——EFCore之Code First

    ASP.NET Core 3.1系列(16)——EFCore之Code First

    前一篇博客介紹了 EFCore 中的 DB First 開發(fā)模式,該模式可以根據(jù)數(shù)據(jù)庫生成實(shí)體類和數(shù)據(jù)庫上下文,因此適用于數(shù)據(jù)庫已經(jīng)存在的場(chǎng)景。而與之相對(duì)應(yīng)的, Code First 主要是根據(jù)自定義的實(shí)體類和數(shù)據(jù)庫上下文反向構(gòu)建數(shù)據(jù)庫,因此也可以看做是 DB First 的逆過程,下面開始介紹

    2024年01月17日
    瀏覽(23)
  • ASP.NET Core 3.1系列(15)——EFCore之DB First

    ASP.NET Core 3.1系列(15)——EFCore之DB First

    本文開始介紹一些關(guān)于 Entity Framework Core 的內(nèi)容。在 EFCore 中,常用的為 DB First 模式和 Code First 模式,下面就來介紹一下如何在 EFCore 中使用 DB First 模式生成實(shí)體類和數(shù)據(jù)庫上下文。 在 SQL Server 中新建一個(gè)數(shù)據(jù)庫 Dao ,執(zhí)行如下語句,創(chuàng)建 Country 和 Province 數(shù)據(jù)表。 運(yùn)行結(jié)果如

    2024年02月15日
    瀏覽(29)
  • asp.net core 6.0 efcore +sqlserver增刪改查的demo

    下面是一個(gè)使用ASP.NET Core 5.0和Entity Framework Core進(jìn)行增刪改查操作的示例。 首先,創(chuàng)建一個(gè)空的ASP.NET Core 6.0 Web應(yīng)用程序項(xiàng)目。 然后,安裝以下NuGet包: Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools 接下來,創(chuàng)建一個(gè)數(shù)據(jù)庫上下文類,用于定義實(shí)體類和數(shù)據(jù)庫連接

    2024年02月13日
    瀏覽(27)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——模塊管理升級(jí)(六十)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——模塊管理升級(jí)(六十)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——定義倉儲(chǔ)并實(shí)現(xiàn) (四) abp(net core)+easyui+efcore實(shí)

    2023年04月09日
    瀏覽(92)
  • 在NET8中使用簡化的 AddJwtBearer 認(rèn)證

    系統(tǒng)版本: win10 .NET SDK: NET8 開發(fā)工具:vscode 參考引用:使用 dotnet user-jwts 管理開發(fā)中的 JSON Web 令牌 注意:以下示例中的端口、token等需替換成你的環(huán)境中的信息 運(yùn)行以下命令來創(chuàng)建一個(gè)空的 Web 項(xiàng)目,并添加 Microsoft.AspNetCore.Authentication.JwtBearer NuGet 包: 將 Program.cs 的內(nèi)容替

    2024年02月05日
    瀏覽(25)
  • abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——組織管理升級(jí)之下(六十二)

    abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——組織管理升級(jí)之下(六十二)

    A bp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)目錄 abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——ABP總體介紹(一) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——解決方案介紹(二) abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——領(lǐng)域?qū)觿?chuàng)建實(shí)體(三) ? abp(net core)+easyui+efcore實(shí)現(xiàn)倉儲(chǔ)管理系統(tǒng)——

    2023年04月23日
    瀏覽(49)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包