日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

EntityFramework Core数据查询

發(fā)布時間:2025/3/21 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EntityFramework Core数据查询 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

本節(jié)我們再來講講EF Core,本節(jié)算是回歸基礎(chǔ)吧,當(dāng)前項目EF Core還是處于1.1版本中,后續(xù)等待.net core等版本穩(wěn)定了全部會更新到2.0版本中,到時再來更新相關(guān)文章分享給大家。

相關(guān)數(shù)據(jù)加載

在EF中一直以來都是通過導(dǎo)航屬性來加載一個實體的相關(guān)數(shù)據(jù),在EF Core中加載相關(guān)數(shù)據(jù)有以下三種模式:

饑餓加載

來自數(shù)據(jù)庫相關(guān)聯(lián)數(shù)據(jù)的加載也會作為實體的一部分進行加載。

我們通過Include方法來進行饑餓加載實體相關(guān)聯(lián)的數(shù)據(jù),如下:

using (var context = new BloggingContext()) {var blogs = context.Blogs.Include(blog => blog.Posts).ToList(); }

當(dāng)然我們也可以同時關(guān)聯(lián)實體的多個屬性,如下:

using (var context = new BloggingContext()) {var blogs = context.Blogs.Include(blog => blog.Posts).Include(blog => blog.Owner).ToList(); }

同時我們也可以類似層級一樣來加載一個實體的相關(guān)的實體的相關(guān)聯(lián)實體,通過ThenInclude,如下:

using (var context = new BloggingContext()) {var blogs = context.Blogs.Include(blog => blog.Posts).ThenInclude(post => post.Author).ToList(); }

是不是只要我們加上了Include方法就會加載一個實體相關(guān)聯(lián)的數(shù)據(jù)呢,如下:

using (var context = new BloggingContext()) {var blogs = context.Blogs.Include(blog => blog.Posts).Select(blog => new{Id = blog.BlogId,Url = blog.Url}).ToList(); }

很明顯我們最后只是投影了Blog中的幾個字段,所以此時會忽略對Post導(dǎo)航屬性的查詢。當(dāng)在這種情況下對于最后返回的數(shù)據(jù)未包含相關(guān)聯(lián)數(shù)據(jù)時在日志文件中會進行提醒,在這種情況下我們很明確知道會忽略對導(dǎo)航數(shù)據(jù)的加載,同時也不會拋出異常,我們可以在日志文件中對該項處理進行忽略,如下:

services.AddDbContext<EFCoreContext>(options =>{options.UseSqlServer(sqlStr, d => d.MigrationsAssembly("StudyEFCore")).ConfigureWarnings(warnings => warnings.Throw(CoreEventId.IncludeIgnoredWarning);});

顯式加載

來自數(shù)據(jù)庫相關(guān)聯(lián)數(shù)據(jù)的加載在稍后會進行加載。

顯式加載只有在EF Core1.1版本上才會被支持,通過DbContext.Entry()來實現(xiàn),如下:

using (var context = new BloggingContext()) {var blog = context.Blogs.Single(b => b.BlogId == 1);context.Entry(blog).Collection(b => b.Posts).Load();context.Entry(blog).Reference(b => b.Owner).Load(); }

當(dāng)然我們也可以通過Linq來加載相關(guān)聯(lián)數(shù)據(jù),如下:

using (var context = new BloggingContext()) {var blog = context.Blogs.Single(b => b.BlogId == 1);var postCount = context.Entry(blog).Collection(b => b.Posts).Query().Count(); }

延遲加載

來自數(shù)據(jù)庫相關(guān)聯(lián)數(shù)據(jù)的加載當(dāng)導(dǎo)航屬性被訪問時會被加載,在EF Core中可能將會實現(xiàn)。

客戶端數(shù)據(jù)評估進行翻譯?

在EF Core中不像之前EF版本對于在lambda表達式中對數(shù)據(jù)進行操作此時將導(dǎo)致無法翻譯從而出錯,但是在EF Core中對于一些簡單的數(shù)據(jù)可以進行了翻譯,這一點還是讓我們有了一點期待。我們一起來看看。

public class Sample{public static string StandardizeUrl(string url){url = url.ToLower();if (!url.StartsWith("http://")){url = string.Concat("http://", url);}return url;}public static void Run(){using (var context = new BloggingContext()){var blogs = context.Blogs.OrderByDescending(blog => blog.Rating).Select(blog => new{Id = blog.BlogId,Url = StandardizeUrl(blog.Url)}).ToList();}using (var context = new BloggingContext()){var blogs = context.Blogs.Where(blog => StandardizeUrl(blog.Url).Contains("dotnet")).ToList();}}}

同時這種對于代碼進行評估進行翻譯的情況我們也可以進行禁用,如下:

services.AddDbContext<EFCoreContext>(options =>{options.UseSqlServer(sqlStr, d => d.MigrationsAssembly("StudyEFCore")).ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));});

總結(jié)

本文簡單講解了下EF Core中相關(guān)數(shù)據(jù)加載的三種模式以及對于客戶端數(shù)據(jù)能夠進行簡單的翻譯,我們下節(jié)再見。

轉(zhuǎn)載于:https://www.cnblogs.com/CreateMyself/p/7108123.html

總結(jié)

以上是生活随笔為你收集整理的EntityFramework Core数据查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。