擴充 .Context.cs 覆寫 SaveChanges() 顯示詳細的驗證錯誤訊息
1. 在Models資料夾底下新增資料夾,建議取名Extend
2. 新增class,檔名取相同名稱方便識別,如 Model1.Context.cs
3. 檔案範例內容如下
namespace xxx.Models
{
public partial class xxxEntities
{
public override int SaveChanges()
{
try
{
return base.SaveChanges();
}
catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
var errorMessages = ex.EntityValidationErrors
.SelectMany(x => x.ValidationErrors)
.Select(x => x.ErrorMessage);
// Join the list to a single string.
var fullErrorMessage = string.Join("; ", errorMessages);
// Combine the original exception message with the new one.
var exceptionMessage = string.Concat(ex.Message, "驗證錯誤訊息: ", fullErrorMessage);
// Throw a new DbEntityValidationException with the improved exception message.
throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
}
}
}
}
參考來源
訂閱:
張貼留言 (Atom)
自訂權限驗證機制
// 使用 filter [Route("api/[controller]")] [ApiController] [Authorize] [TypeFilter(typeof(CustomAsyncAuthorizationFilter))] public c...
-
1. 設定檔案下載儲存位置為 C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default\Cache 2. 勾選"下載每個檔案前詢問儲存位置" 3. 針對不要下載的檔案類型於第一...
-
自動設定欄寬 sheet.Cells.AutoFitColumns(3, 20); // 必須設定 min 跟 max 才會正常作用 凍結欄位 sheet.View.FreezePanes(4, 4); 標題列 ws.PrinterSettings.RepeatRo...
-
使用 FreeSpire.XLS ... ep.Save(); using (var workbook = new Workbook()) using (var memStream = new MemoryStream()) { workbook.LoadFromSt...
沒有留言:
張貼留言