顯示具有 .NET 標籤的文章。 顯示所有文章
顯示具有 .NET 標籤的文章。 顯示所有文章

2025/04/06

[EF Core] 解決 Decimal 類型出現 No store type was specified for the decimal property 警告訊息

markdown ### 版本 - .NET 8 - EF Core 9 ### 問題 在 EF Core 使用 decimal 類型的屬性時,一直跳出 No store type was specified for the decimal property 的警告訊息 ### 解決方法 在屬性上方加上 [Precision(18, 2)] ```cs [Precision(18, 2)] public decimal Price { get; set; } ``` > 需要 .NET EF Core 6 以上的版本才支援 ### 參考 - MS Learn - PrecisionAttribute Class - Decimal precision and scale in EF Code First

2025/01/24

[Azure] ASP.NET Core 專案部署到 Web App for Containers 問題除錯心得

markdown ### 前言 因為新專案要求要用 Docker,本地開發端的問題通常不大,開專案也都有 Template,往往都是實際要部署上線時會遇到一些狀況。 趁著記憶猶新,整理一下部署心得 ### 開發環境 - Windows 11 - Docker Desktop v4.37.1 - Visual Studio 2022 (v17.11.x -- 原版本忘了,因部署 Bug 問題,後來升級到 v17.12.4) - ASP.NET Core Web App (Model-View-Controller) 專案 - .NET 8.0 ### 選擇部署到 Azure 的服務 將專案部署到 Azure 上,我選擇的服務如下: - Azure Container Registry (ACR) - Azure App Service (Web App for Containers) - Azure SQL Database - Azure Storage

2024/06/18

[C#][WebAPI] Json.NET / System.Text.Json 序列化物件中忽略 null 值的屬性

markdown 近期遊走在 ASP.NET MVC 5 / Web API 2 以及 ASP.NET Core MVC (.NET) 的專案中 對於由 API 讀取進來的物件在 .NET Framework 與 .NET 下處理的方式不同, 需要記錄一下以便日後翻查

場景需求

使用場景是把接收進來的物件內容轉換做一個清洗,只要從 Web API 接進來的物件屬性值為 null 的都不保留 例如: 有一個 Log 物件如下 ```C# var log = new Log { Timestamp = "2024-06-16T09:22:01.5120098Z", Level = "Information", Message = "This is test message", Exception = null }; ``` 那麼經過清洗過後的字串內容就不會包含 Exception 空值的屬性 ```Json { "Timestamp":"2024-06-16T09:22:01.5120098Z", "Level":"Information", "Message":"This is test message" } ``` 以下即是在 ASP.NET MVC 5 及 ASP.NET Core 不同環境的處理方式

2023/02/26

[Visual Studio 2022] v17.5 新功能 - ASP.NET Core 輸出整合內建 Terminal

markdown ### 前言 有在開發 ASP.NET Core 網站的朋友們,在 Visual Studio 上開發時,應該對於那個跳出來不能關掉的 Console 視窗感到很煩。 若是本身就用 VS Code 來開發或是原先就在 Terminal 下操作,是不會有這種困擾的。 一直想敲碗很久的功能,終於在 v17.5 的版本聽見開發者的許願~ 現在能把 output 到外跳出來的視窗導到內建的 Terminal 視窗去囉。

2023/02/20

[.NET] 升級 ASP.NET MVC 至 ASP.NET Core MVC 的注意事項

markdown ### 前言 繼上篇 [如何使用 .NET Upgrade Assistant 升級舊 ASP.NET MVC 專案](https://devmanna.blogspot.com/2023/02/howto-upgrade-using-net-upgrade-assistant.html) 所遇到的升級問題再做一個小小的注意事項筆記 ### 網站檔案路徑 在 ASP.NET MVC 可以透過 `Server.MapPath()` 來指定在伺服器上的路徑 例如: ```cs public class HomeController : Controller { public ActionResult Index() { string docsPath = Server.MapPath("~/docs"); //... } } ``` 而在 ASP.NET Core MVC 需要經由注入 Microsoft.AspNetCore.Hosting.IWebHostEnvironment 來讀取環境變數 例如: ```cs public class MyService { public MyService(IWebHostEnvironment env) { string docsPath = $@"{env.CotnentRootPath}/docs"; } } ```

2023/02/19

[.NET] 如何使用 .NET Upgrade Assistant 升級舊 ASP.NET MVC 專案

markdown ### 問題 進入 .NET 7 的今日,手邊仍有許多舊的專案,要如何將舊專案升級呢? 微軟提供了 .NET Upgrade Assistant 工具來協助升級舊專案。 讓我們一起來一步步升級吧! ### 升級專案 舊專案環境: - ASP.NET MVC 5.2.9 - .NET Framework 4.7.2 想要升級目標專案至 .NET 7

2022/12/18

[.NET] 更新信任 ASP.NET Core 開發憑證

markdown ### 前言 每年都要更新 ASP.NET Core 本機端自簽的開發憑證,剛好就在不久前憑證過期。透過剛結束的 [.NET Conf Taiwan 2022](https://dotnetconf.study4.tw/) 從保哥那裡學到一招更新方法。立即現學現賣。 ### 過期的憑證 寫部落格的同時恰巧經歷了正常到過期的憑證變化。 ``` $ dotnet dev-certs https --check --trust ``` 原本的 `A valid certificate was found` 變成 `The following certificates were found, but none of them is trsuted.`

> 我的開發環境: Windows 10,若是其他的平台可以查詢 [Microsoft Learn - 使用 .NET CLI 產生自我簽署憑證](https://learn.microsoft.com/zh-tw/dotnet/core/additional-tools/self-signed-certificates-guide?WT.mc_id=DT-MVP-5002629) ### 更新步驟 1. 先清掉舊的憑竳 ``` $ dotnet dev-certs https --clean ``` 2. 再重新建立新憑證 ``` $ dotnet dev-certs https --trust ``` ### 見證更新過程 正常 -> 過期 -> 清除 -> 重建 -> 正常

### 相關資料 - [使用 .NET CLI 產生自我簽署憑證](https://learn.microsoft.com/zh-tw/dotnet/core/additional-tools/self-signed-certificates-guide?WT.mc_id=DT-MVP-5002629)

2019/03/14

[.NET] 用 Ical.Net 建立 iCalendar ics 日曆數據交換檔案

markdown 今天在專案中要產生 *.ics 的日曆數據交換檔案提供下載 打鐵趁熱記錄一下,以免又要石沈大海 目前採用的是在 NuGet 的 [Ical.Net](https://www.nuget.org/packages/Ical.Net/) 套件, 版本 v4.1.10 建立 CalendarEvent 超簡單
*.ics 內容如下
在 Windows 10 的行事曆開啟 *.ics 的呈現內容
### 範例程式 (採用 Web API)
### 參考資料 - [WiKi - iCalendar](https://zh.wikipedia.org/wiki/ICalendar) - [NuGet - Ical.Net](https://www.nuget.org/packages/Ical.Net/) - [Ical.Net Wiki](https://github.com/rianjs/ical.net/wiki)

2017/01/01

Happy New Year 2017

Happy New Year 2017!

硬是要在 2017 年的第一天寫上一篇 blog

今年首要的執行專案全落在 ASP.NET MVC 5 的網站開發上

每開發一次專案,總會練功鍛鍊出新想法,新作法,要變得更好就要多吸收新知。

YouTube 一直是我學習的好朋友~

今天專注在 Unit Testing and TDD 的議題上

分享一下今天看的教學影片

https://www.youtube.com/playlist?list=PLdbkZkVDyKZXqPu-xDFkzuP66QijGeewz

2016/09/02

[.NET] 解決 EF 錯誤: there is already an open datareader associated with this command which must be closed first

筆記錯誤解法, 供未來翻查

情境:

foreach 中又有巢狀的查詢物件條件, 造成 lazy loading 出現錯誤


錯誤訊息: 
There is already an open datareader associated with this command which must be closed first.

2010/05/03

[.NET] Bitmap to byte array

byte[] bytes = (byte[])System.ComponentModel.TypeDescriptor.GetConverter(bmp).ConvertTo(bmp, typeof(byte[]));

參考網址: Bitmap to byte array

2009/05/12

String.Format 字串格式化

常要做字串格式,利用 String.Format() 可以快速達到效果,不用為了組字串或特殊格式呈現,東少一個連接字串符號,右少一個引號... 若格式改來改去也很麻煩~ String.Format 可省去很多麻煩,彈性又大~~

String.Format 語法:
{index[,length][:formatString]}
e.g. String.Format("{0:d}", Now)
output: 5/13/2009


以下為字串格式化總覽:

標準數字格式化
(C) Currency:  {0:C}
(D) Decimal: {0:D}
(E) Scientific: {0:E}
(F) Fixed point: {0:F}
(G) General: {0:G}
(N) Number: {0:N}
(P) Percent: {0:P}
(R) Round-trip: {0:R}
(X) Hexadecimal: {0:X}

標準日期/時間格式化
(d) Short date:                  {0:d}
(D) Long date: {0:D}
(t) Short time: {0:t}
(T) Long time: {0:T}
(f) Full date/short time: {0:f}
(F) Full date/long time: {0:F}
(g) General date/short time: {0:g}
(G) General date/long time: {0:G}
(M) Month: {0:M}
(R) RFC1123: {0:R}
(s) Sortable: {0:s}
(u) Universal sortable: {0:u} (invariant)
(U) Universal full date/time: {0:U}
(Y) Year: {0:Y}
0 v.s. #
另外 {0:00.00} 或 {0:##.##} 在利用數字格式化也常使用
用 0 就是補 0,而 # 在末數為 0 時就不顯示

e.g.
{00.00} 01.50; 11.30; 01.11; 123.30; 123.33
{##.##} .4; 1.4; 1; 1.33; 11.3; 123.33

多參數字串:
e.g. String.Format("{0}, Now is {1:d}", "Hello", Now)
output: Hello, Now is 5/13/2009


延伸閱讀:
MSDN - String..::.Format Method (String, Object)
ASP.NET设置数据格式与String.Format使用总结
Easily format string output with String.Format
ASP.NET string.Format 格式參數

2009/04/18

.NET Coding Standard

由 Clint Edmonson (微軟工程師) 釋出的 .NET 程式命名規則: C# 及 VB
下載: Reference Coding Standards

建立大家都認同的命名規則並遵守, 不管在日後維護或是共同開發都有益處

2007/11/22

MySQL & .NET MySQL connector & Chinese Support

I just encounter a serious problem when inserting chinese data into MySQL using .NET MySQL connector. It shows like ????.

OK...Here is the solution:

Change Connection String and add charset=utf8:

Example:
Database=[dbname];Data Source=[source];User Id=[userid];Password=[password];charset=utf8