發表文章

目前顯示的是有「EntityFramework Core」標籤的文章

efcore預設會將不該轉到資料庫的model轉換到資料庫如何忽略?

圖片
於該Model類別上標記[NotMapped] 即可忽略 使ViewModel不會同樣一起migrate到DB中 還有一種可能就是去檢查你的DbContext裡面是不是有誤定義DbSet包進ViewModel EF Core Ignore https://www.learnentityframeworkcore.com/configuration/fluent-api/ignore-method ignore one table with ef-core migration https://stackoverflow.com/questions/64979208/ignore-one-table-with-ef-core-migration How to exclude one table from automatic code first migrations in the Entity Framework? https://stackoverflow.com/questions/22038924/how-to-exclude-one-table-from-automatic-code-first-migrations-in-the-entity-fram EF Core 筆記 2 - Model 設計 https://blog.darkthread.net/blog/ef-core-notes-2/

EFCore CodeFirst 產生資料庫可能常遇到的怪問題_More than one DbContext was found._An error occurred using the connection to database

圖片
  More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands. 這代表專案可能包含兩種資料庫上下文的Class (繼承自DbContext的衍生類別) 若專案有用到Identity也算一種 這時就要多串-context 指定相應DbContext子類名稱 An error occurred using the connection to database '{資料庫名稱}' on server 'localhost'. 這代表資料庫連線配置錯誤喔!!

could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable'

圖片
  通常會發生在 第一個集合 left join 第二個集合這邊 第一個集合這裡可能要先撈取出來(.ToList() ....etc) 然後也要記得把握住前後集合順序 from 數量少的join 數量多的on 即可修正 Ref: could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable' https://www.cnblogs.com/xiaogaopan/p/15384353.html could not be translated. Either rewrite the query in a form that can be translated https://blog.csdn.net/qq_42799562/article/details/120162877?ops_request_misc=&request_id=&biz_id=102&utm_term=could%20not%20be%20translated.%20Eithe&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-120162877.142^v56^control_1,201^v3^add_ask&spm=1018.2226.3001.4187

Unable to track an instance of type 'XXX實體' because it does not have a primary key. Only entity types with primary keys may be tracked.

圖片
  某張table若在透過EF ORM轉化完 一定要設置PK 否則變更追蹤機制是會失效的

EntityFramework Core筆記(4)_延遲加載(執行)_IQueryable跟IEnumerable差異

圖片
  延遲加載(執行) : 屬於預設情況,當你查詢 一個集合就延遲,等到真正要使用存取該資料才去DB抓。 預設從DbContext存取的表資料回傳只要還沒去call .ToList() 或是 .ToArray() 等方法 都是回傳IQueryable<T> 都還不會真正跟DB有交握。 優點: 就在於可以降低阻塞,提升性能 需注意: 加載資料不能夠在DbContext被回收之後才運行 延遲加載(執行)-測試程式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 using EFCoreDbFirst_ConsoleApp.Models ; using System ; using System.Linq ; namespace EFCoreDbFirst_ConsoleApp { class Program { static void Main ( string [] args) { try { using (CustomerDbContext context = new CustomerDbContext()) { //Lazy Loading IQueryable<Commodity> commodities = context.Commodities.Where(item => item.Id == 20001 ); Console.WriteLine( "開始存取" ); foreach ( var item in commodities) { Console.WriteLine(item.Title); ...

EntityFramework Core筆記(3)_DbFirst指令_監聽SQL語法兩種方式(SQL Server Profiler/LoggerFactory)

圖片
 為了怕之後有新人來 一來要帶人 一來還要趕專案 因此筆記一下工作常用的開發工具 專案.net core主控台新建並安裝好如下四套件 資料庫中目前存在的資料表 如要快速於本機產生此DB及相應Tables 執行如下T-SQL即可 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 USE [master] GO /****** Object: Database [AdvancedCustomerDB] Script Date: 2021/10/29 11:40:58 ******/ CREATE DATABASE [AdvancedCustomerDB] CONTAINMENT = NONE ON PRIMARY ( NAME = N 'AdvancedCustomerDB' , FILENAME = N 'E:\IT_Db\AdvancedCustomerDB.mdf...

.net core 與.net mvc及web api_學習筆記分享_章節大綱

圖片
  章節規劃比較零散 純個人隨興安排 可自行挑選想從哪個部分來展開 .NET MVC系列 ASP.NET MVC(一)_Webform跟MVC差在哪?_MVC專案架構概述 ASP.NET MVC(二)_從頭建立ASP.Net MVC應用程式_循序漸近式理解Controller跟View互動 ASP.NET MVC(三)_Model跟View之間透過Controller的互動 ASP.NET MVC(四)_路由的觀念解釋 ASP.NET MVC(五)_Razor語法筆記(一) ASP.NET MVC(六)_ViewData,ViewBag,TempData用法與差異比較 ASP.NET MVC(七)_表單Get和Post_將View輸入資料傳到Contoller ASP.NET MVC(八)_表單的屬性添加 ASP.NET MVC(九)_Model介紹(1)_單筆Model傳入至View_基本DataAnnotations介紹跟檢視上DisplayFor跟DisplayNameFor使用 ASP.NET MVC(十)_Model介紹(2)_檢視接收多筆MVC Model資料 ASP.NET MVC第011天_查詢結果用PagedList分頁呈現 ASP.NET MVC第012天_ASP.NET Identity使用筆記_初始配置到註冊篇 ASP.NET MVC第013天_ASP.NET Identity使用筆記_註冊完後的郵件有效性確認 ASP.NET MVC第014天_ASP.NET Identity使用筆記_使用者登入_Claims-based identity理解 ASP.NET MVC第015天_是不是用了Identity後就一定要接受它自動建立的DB跟命名?不想用到一個DB以上怎麼辦 ASP.NET MVC第016天_註冊用戶資料查詢結果顯示與單筆編輯刪除 ASP.NET MVC第020天_HtmlHelper介紹_用法筆記需要留意的細節 ASP.NET MVC第021天_Ajax Helper_AjaxOptions,AjaxExtensions,ActionLink,InsertionMode使用筆記part1 ASP.NET MVC第022天_Ajax Helper_Ajax.BeginForm使用筆記part2 ASP.NET MVC第023...