前言
EF Core是微軟官方提供的ORM框架。EF Core不僅可以操作Microsoft SQL Server、MySQL、Oracle、PostgreSQL等數(shù)據(jù)庫,而且可以操作Azure Cosmos DB等NoSQL數(shù)據(jù)庫
一、EF Core環(huán)境搭建
前提條件:已經(jīng)完整安裝了Microsoft SQL Server
下面是一個實際操作EF Core的演示
- 這是項目最終的目錄,這里需要關注的就是
.cs
文件
- 首先新建一個
.NET Core
控制臺項目,然后在項目中創(chuàng)建Book實體類
public class Book
{
public long Id { get; set; } //主鍵
public string Title { get; set; }//標題
public DateTime PubTime { get; set; }//發(fā)布日期
public double Price { get; set; }//單價
public string AuthorName { get; set; }//作者名字
}
- 為項目安裝NutGet包
Microsoft.EntityFrameworkCore.SqlServer
安裝命令
Install-Package Microsoft.EntityFrameworkCore.SqlServer
- 創(chuàng)建實現(xiàn)了
IEntityTypeConfiguration
接口的實體類的配置類BookEntityConfig
class BookConfig : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
//表示實體類對應的數(shù)據(jù)庫表的名字為T_Books
builder.ToTable("T_Books");
//這里沒有配置各個屬性子在數(shù)據(jù)庫中列名和數(shù)據(jù)類型,EF Core將會默認把屬性的名字作為列名,并且以屬性的類型來推斷數(shù)據(jù)庫表中各列的數(shù)據(jù)類型
}
}
這個配置的作用是配置實體類和數(shù)據(jù)庫表的對應關系,現(xiàn)在可能理解的并不深刻,后面就會明白很多了…
- 創(chuàng)建一個繼承自DbContext類的MyDbContext類(名字沒有要求)
class MyDbContext:DbContext
{
public DbSet<Book> Books { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Server=.;Database=demo1;Trusted_Connection=True;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//從當前程序集中加載所有的IEntityTypeConfiguration
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
}
}
MyDbContext中的Books屬性對應的數(shù)據(jù)庫中的T_Books表,對Books的操作將會反映到數(shù)據(jù)庫的T_Books表中。這種傳承自DbContext的類叫做“上下文” 。OnConfiguring
方法用于對程序要連接的數(shù)據(jù)庫進行配置,這里的配置
optionsBuilder.UseSqlServer("Server=.;Database=demo1;Trusted_Connection=True;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;");
是對本地SQL Server數(shù)據(jù)庫的連接,名為demo1的數(shù)據(jù)庫,如果連接其他的服務器中的數(shù)據(jù)庫,需求去微軟文檔查看相應的連接字符串要求
- 通過NuGet為項目安裝
Microsoft.EnityFrameworkCore.Tools
包,這是為了使用EF Core生成數(shù)據(jù)庫工具
Install-Package Microsoft.EnityFrameworkCore.Tools
- 安裝完之后我們執(zhí)行
Add-Migration
命令會自動在項目的Migrations文件夾中生成C#代碼
這里后面的AddBirth
是自行定義的,是一個名字而已 - 最后執(zhí)行
Update-database
命令那些在Migrations
文件夾中用來創(chuàng)建數(shù)據(jù)庫表的代碼才會被應用到數(shù)據(jù)庫中
這時候我們就可以來查看數(shù)據(jù)庫中是否有對應的表了
二、基本的增刪改查
1.增加數(shù)據(jù)
這里是在Program.cs
中操作了
internal class Program
{
static async Task Main(string[] args)
{
//創(chuàng)建邏輯上的數(shù)據(jù)庫
using (MyDbContext myDbContext = new MyDbContext())
{
Book b1 = new Book
{
AuthorName = "楊中科",
Title = "零基礎學C語音",
Price = 59.8,
PubTime = new DateTime(2019, 3, 1)
};
Book b2 = new Book
{
AuthorName = "Robert Sedgewick",
Title = "算法(第四版)",
Price = 99,
PubTime = new DateTime(2012, 10, 1)
};
Book b3 = new Book
{
AuthorName = "吳軍",
Title = "數(shù)學之美",
Price = 69,
PubTime = new DateTime(2020, 5, 1)
};
Book b4 = new Book
{
AuthorName = "楊中科",
Title = "程序員的SQL金典",
Price = 52,
PubTime = new DateTime(2008, 9, 1)
};
Book b5 = new Book
{
AuthorName = "吳軍",
Title = "文明之光",
Price = 246,
PubTime = new DateTime(2017, 3, 1)
};
//把對象加入邏輯上的表上面
myDbContext.Books.Add(b1);
myDbContext.Books.Add(b2);
myDbContext.Books.Add(b3);
myDbContext.Books.Add(b4);
myDbContext.Books.Add(b5);
//Update-Database
await myDbContext.SaveChangesAsync();
}
}
}
2.查詢數(shù)據(jù)
IQueryable<Book> books = myDbContext.Books.Where(b => b.Price > 80);
foreach(var book in books)
{
Console.WriteLine(book.Title);
}
var Book = myDbContext.Books.Single(b => b.Title == "零基礎學C語言");
Console.WriteLine(Book.AuthorName);
查詢就不需要 await myDbContext.SaveChangesAsync();
了,在Main方法中執(zhí)行即可文章來源:http://www.zghlxwxcb.cn/news/detail-407267.html
3.修改數(shù)據(jù),刪除數(shù)據(jù)
修改數(shù)據(jù)和刪除數(shù)據(jù),都分兩步,第一步就是把數(shù)據(jù)查詢出來,再就是對應操作文章來源地址http://www.zghlxwxcb.cn/news/detail-407267.html
//修改,要對數(shù)據(jù)進行修改,首先先查出來對應數(shù)據(jù),再修改
var b = myDbContext.Books.Single(b => b.Title == "數(shù)學之美");
b.AuthorName = "junwu";
Dog dog = myDbContext.Dogs.Single(b => b.Id == 2);
myDbContext.Dogs.Remove(dog);
await myDbContext.SaveChangesAsync();
到了這里,關于EF Core入門的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!