安裝以下NuGet包
Microsoft.EntityFrameworkCore.SqlServer:SQL server 需要添加包
Microsoft.EntityFrameworkCore.Tools
Newtonsoft.Json:用于Json格式轉(zhuǎn)換
創(chuàng)建一個(gè)實(shí)體類來表示數(shù)據(jù)庫(kù)表。在項(xiàng)目中創(chuàng)建一個(gè)名為Customer.cs的文件,并添加以下代碼
namespace AliWorkbenchProgram.Models
{
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
}
創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)上下文類,用于定義實(shí)體類和數(shù)據(jù)庫(kù)連接配置。在項(xiàng)目中創(chuàng)建一個(gè)名為AppDbContext.cs的文件,并添加以下代碼:
using AliWorkbenchProgram.Models;
using Microsoft.EntityFrameworkCore;
namespace AliWorkbenchProgram.DB
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Customer> Customers { get; set; }
}
}
接下來,配置數(shù)據(jù)庫(kù)連接。打開appsettings.json文件,并添加以下內(nèi)容:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EFCoreDemo;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
然后,在Startup.cs文件的ConfigureServices方法中添加以下代碼,用于配置數(shù)據(jù)庫(kù)上下文的依賴注入:
創(chuàng)建類 DbEntitys 繼承 DbContext
添加包并引用
using Microsoft.EntityFrameworkCore;
using AliWorkbenchProgram.Models;
using Microsoft.EntityFrameworkCore;
namespace AliWorkbenchProgram.DB
{
public class DbEntitys : DbContext
{
/// <summary>
/// 配置連接字符串,每次訪問數(shù)據(jù)庫(kù)之前會(huì)自動(dòng)執(zhí)行此方法,在這里配置連接字符串
/// 相當(dāng)于連接前事件
/// 使用 IOC 注入的方式不實(shí)現(xiàn)此方法
/// </summary>
/// <param name="builder"></param>
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
// 連接字符串
string ConnString = "xxx";
// 連接SqlServer
builder.UseSqlServer(ConnString);
// 連接MySql
//builder.UseMySql(ConnString,new MySqlServerVersion(new Version()));
}
/// <summary>
/// 默認(rèn)構(gòu)造函數(shù) 使用方法與原來一樣
/// </summary>
public DbEntitys() : base() { }
/// <summary>
/// 通過IOC
/// </summary>
/// <param name="options"></param>
public DbEntitys(DbContextOptions<DbEntitys> options) : base(options)
{ }
/*
/// <summary>
/// 無主鍵 視圖
/// </summary>
/// <param name="builder"></param>
protected override void OnModelCreating(ModelBuilder builder)
{
//指定主鍵字段,如果主鍵名稱是id/Id/ID,可以省略
builder.Entity<Tab1>().HasKey("Key");
//指定為無主鍵或視圖,不然可能會(huì)報(bào)錯(cuò)
builder.Entity<view1>().HasNoKey();
builder.Entity<view2>().HasNoKey();
}
//表映射
public virtual DbSet<Tab1> Tab1 { get; set; }
public virtual DbSet<Tab2> Tab2 { get; set; }
*/
//表映射
public virtual DbSet<Student> Students { get; set; }
//public virtual DbSet<Tab2> Tab2 { get; set; }
}
}
使用
IOC
在?Program.cs
// ↓↓↓↓↓ 在此范圍內(nèi) ↓↓↓↓↓
var ConnString = "xxx";
//注入
builder.Services.AddDbContext<DbEntitys>(x => x.UseSqlServer(ConnString));
// ↑↑↑↑↑ 在此范圍內(nèi) ↑↑↑↑↑
在控制器 xxxController文章來源:http://www.zghlxwxcb.cn/news/detail-724251.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-724251.html
// <summary>
/// 數(shù)據(jù)庫(kù)操作實(shí)例
/// </summary>
private readonly DbEntitys _context;
/// <summary>
/// 構(gòu)造函數(shù)獲取實(shí)例
/// </summary>
/// <param name="db"></param>
public StudentController(DbEntitys db)
{
_context = db;
}
[HttpGet(Name = "GetData")]
public IEnumerable<Student> Get()
{
//查詢數(shù)據(jù)
return _context.Students.ToList();
}
到了這里,關(guān)于.NET Core/.NET6 使用DbContext 連接數(shù)據(jù)庫(kù),SqlServer的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!