Entity Framework Core介绍(1)
介紹
Entity Framework (EF) Core 是輕量化、可擴展和跨平臺版的常用 Entity Framework 數(shù)據(jù)訪問技術(shù)。
EF Core 可用作對象關(guān)系映射程序 (O/RM),以便于 .NET 開發(fā)人員能夠使用 .NET 對象來處理數(shù)據(jù)庫,這樣就不必經(jīng)常編寫大部分數(shù)據(jù)訪問代碼了。
EF Core 支持多個數(shù)據(jù)庫引擎,請參閱數(shù)據(jù)庫提供程序了解詳細信息。
模型
對于 EF Core,使用模型執(zhí)行數(shù)據(jù)訪問。?模型由實體類和表示數(shù)據(jù)庫會話的派生上下文構(gòu)成,用于查詢和保存數(shù)據(jù)。?有關(guān)詳細信息,請參閱創(chuàng)建模型。
可從現(xiàn)有數(shù)據(jù)庫生成模型,手動編碼模型使之與數(shù)據(jù)庫相匹配,或使用 EF 遷移基于模型創(chuàng)建數(shù)據(jù)庫(并在模型隨時間推移發(fā)生更改后進行相應(yīng)改進)。
using Microsoft.EntityFrameworkCore; using System.Collections.Generic;namespace Intro {public class BloggingContext : DbContext{public DbSet<Blog> Blogs { get; set; }public DbSet<Post> Posts { get; set; }protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");}}public class Blog{public int BlogId { get; set; }public string Url { get; set; }public int Rating { get; set; }public List<Post> Posts { get; set; }}public class Post{public int PostId { get; set; }public string Title { get; set; }public string Content { get; set; }public int BlogId { get; set; }public Blog Blog { get; set; }} }?
查詢
使用語言集成查詢 (LINQ) 從數(shù)據(jù)庫檢索實體類的實例。?有關(guān)詳細信息,請參閱查詢數(shù)據(jù)。
using (var db = new BloggingContext()) {var blogs = db.Blogs.Where(b => b.Rating > 3).OrderBy(b => b.Url).ToList(); }?
保存數(shù)據(jù)
使用實體類的實例在數(shù)據(jù)庫中創(chuàng)建、刪除和修改數(shù)據(jù)。?有關(guān)詳細信息,請參閱保存數(shù)據(jù)。
using (var db = new BloggingContext()) {var blog = new Blog { Url = "http://sample.com" };db.Blogs.Add(blog);db.SaveChanges(); }?
后續(xù)步驟
有關(guān)介紹性教程,請參閱?Entity Framework Core 入門。
?
官方文檔:https://docs.microsoft.com/zh-cn/ef/core/
?
asp.net?core 交流群:787464275?歡迎加群交流
如果您認為這篇文章還不錯或者有所收獲,您可以點擊右下角的【推薦】按鈕精神支持,因為這種支持是我繼續(xù)寫作,分享的最大動力!
聲明:原創(chuàng)博客請在轉(zhuǎn)載時保留原文鏈接或者在文章開頭加上本人博客地址,如發(fā)現(xiàn)錯誤,歡迎批評指正。凡是轉(zhuǎn)載于本人的文章,不能設(shè)置打賞功能,如有特殊需求請與本人聯(lián)系!
微信公眾號:歡迎關(guān)注? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?QQ技術(shù)交流群:?歡迎加群
? ? ? ? ? ? ? ??
LouieGuo總結(jié)
以上是生活随笔為你收集整理的Entity Framework Core介绍(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计数据库的三范式
- 下一篇: 【翻译】CodeMix使用教程(三):E