2023年7月11日 星期二

EF 圖表加入新table 無效

此為 visual studio 某一版之後產生的bug,暫時性解法如下

Start Notepad in administrator mode, and open the file (substituting Community for Professional or Enterprise depending on your version):

C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude

Update the following code in DynamicTextTransformation (approx line 1920) and change:

_generationEnvironment = type.GetProperty(“GenerationEnvironment”, BindingFlags.Instance | BindingFlags.NonPublic);

to

_generationEnvironment = type.GetProperty(“GenerationEnvironment”, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

2023年4月6日 星期四

entity framework 刪除 table record 相依 table 殘留現象

entity framework 刪除 table record 無法針對相依 table 套用 資料庫刪除規則:重疊顯示
若相依table 的欄位不允許null 則會刪除失敗,若允許null 則會改為 null
必須手動刪除相依資料!!

※ EF6 db first 會有這問題,不確定 core 和 code first 是否有相同情形

2023年3月10日 星期五

2023年3月9日 星期四

一段時間未操作系統,下一次動作會卡頓

資料庫連接字串設定 Min Pool Size 避免連接池中的未使用連接在一段時間(大約 4-8 分鐘)後被釋放,因為沒有可用連接而重新產生連接會等待數秒鐘

參考資料

調整 IIS 應用程式集區進階設定,把閒置逾時動作改成Suspend ,避免集區重啟造成連接池消失

參考資料

2023年2月9日 星期四

網頁列印實作

var mywindow = window.open("", "PRINT");
mywindow.document.body.innerHTML = '...';
$(mywindow.document.getElementById("xxx")).xxx('...'); // 使用 jquery 取得新視窗內的物件
mywindow.focus();
mywindow.print();
mywindow.close(); 

若要有分頁效果,可套用以下css
.pagebreak {
        page-break-before: always;
}

2023年2月7日 星期二

圖片處理

改變大小

※會轉為 jpeg 格式
public static byte[] resizeImage(byte[] imagearray, int maxsize)
{
using (var ms = new System.IO.MemoryStream(imagearray))
using (var image = (Image)new Bitmap(Image.FromStream(ms)))
{
decimal scale = ((decimal)image.Width) / image.Height;
Size size = default;
if (scale > 1)
{
int w = maxsize;
int h = (int)(w / scale);
size = new Size(w, h);
}
else
{
int h = maxsize;
int w = (int)(h * scale);
size = new Size(w, h);
}
byte[] arr;
Bitmap bmp = new Bitmap(image, size);
using (var ms1 = new System.IO.MemoryStream())
{
bmp.Save(ms1, ImageFormat.Jpeg);
arr = ms1.ToArray();
return arr;
}
}
}

寫入文字

var newBitmap = new Bitmap(image.Width, (int)(image.Height + fontsize));
newBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(newBitmap))
{
graphics.Clear(Color.White);
graphics.DrawImage(image, 0, 0);
using (Font font = new Font(fontname, fontsize, GraphicsUnit.Pixel))
{
var size = graphics.MeasureString(text, font);
var rect = new RectangleF(x, y, size.Width, size.Height);
graphics.FillRectangle(Brushes.White, rect);
graphics.DrawString(text, font, Brushes.Blue, new PointF(image.Width / 2 - fontsize * 13 / 3, image.Height - size.Height / 2));
}
}

文字轉圖片

Bitmap bmp = new Bitmap(width, Height);
using (Graphics graphics = Graphics.FromImage(bmp))
using (Font font = new Font(fontname, fontsize))
{
graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height);
graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0);
graphics.Flush();
}

2023年2月4日 星期六

2023年1月6日 星期五

bootstrap5 學習心得

toast 用法

html
====
<div class="toast-container position-fixed top-0 end-0 p-3">
<div id="toast1" class="toast hide text-white bg-success ">                
<div class="toast-body">
{{toast訊息}}
</div>
</div>
        <div id="toast2" class="toast hide text-white bg-danger " data-bs-autohide="false">
                <div class="toast-body">
                    {{toast訊息}}
                </div>
        </div>
</div>

javascript
====
this.toast訊息 = 訊息;
var toastEl = document.querySelector(`#toast1`);
(new bootstrap.Toast(toastEl)).show();    

radio & checkbox 美化

<div class="form-check"><label class="form-check-label"><input class="form-check-input" type="radio" value="1" />{{日期時間}}</label><label class="form-check-label"><input class="form-check-input" type="checkbox" style="margin-left:1em" />登記午餐</label></div>
<div class="form-check"><label class="form-check-label"><input class="form-check-input" type="radio" value="2" />下次再邀約</label></div>

<style>
.form-check-input {
margin-right: 0.5em;
}
</style>

2023年1月2日 星期一

升級到 EF 7 遇到的問題

呼叫 savechanges() 遇到錯誤

Could not save changes because the target table has database triggers. Please configure your entity type accordingly, see https://aka.ms/efcore-docs-sqlserver-save-changes-and-triggers for more information. : 如果 DML 陳述式包含 OUTPUT 子句但不含 INTO 子句,陳述式的目標資料表 'OrderItem' 就不可以有任何啟用的觸發程序。

這是因為EF7 使用新的技術寫入資料庫,但 sql server table 有trigger 時不支援此技術,可改為舊的方式處理

參考資料

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...