2021-04-07
ValueObject as Entity ID, EntityTypeConfiguration Sample
markdown
問題:https://stackoverflow.com/questions/53328201/ef-core-non-primitive-type-value-object-as-primary-key
參考來源:https://github.com/dotnet/efcore/issues/13669
```
public class User
{
public UserId Id { get; set; }
public Credentials Credentials { get; set; }
}
public class UserId
{
private UserId()
{
}
private UserId(long id)
{
Id = id;
}
public long Id { get; private set; }
public static implicit operator long(UserId beaconId)
{
return beaconId.Id;
}
public static implicit operator UserId(long id)
{
return new UserId(id);
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
var userId = (UserId) obj;
return this.Id == userId.Id;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
public class TestDbContext: DbContext
{
public TestDbContext(DbContextOptions options): base(options)
{
}
public DbSet Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var userModelBuilder = modelBuilder.Entity();
userModelBuilder.Property(x => x.Id).HasConversion(x => x.Id, value => new UserId(value)).HasColumnName("Id").IsRequired();
userModelBuilder.OwnsOne(x => x.Credentials, c =>
{
c.Property(x => x.Email);
c.Property(x => x.Password);
});
}
}
```
2021-03-31
dotnet core get k8s self POD name
markdown
k8s 中的 dotnet core 應用取得 POD name
三種方式取得的結果相同
```
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("MachineName:"+Environment.MachineName);
Console.WriteLine("HostName:"+ System.Net.Dns.GetHostName());
Console.WriteLine("ENV K8S_POD_NAME:"+Environment.GetEnvironmentVariable("K8S_POD_NAME"));
await Task.Run(() =〉 Thread.Sleep(Timeout.Infinite));
}
}
```
result on dotnet 3.1.11-alpine3.12
```
MachineName:mqsend-7d9579cc47-kld6v
HostName:mqsend-7d9579cc47-kld6v
ENV K8S_POD_NAME:mqsend-7d9579cc47-kld6v
```
ENV需要在deployment時額外加入參考
```
spec:
containers:
- env:
- name: K8S_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
```
ref
- https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-container-fields-as-values-for-environment-variablesƒ
2021-01-29
Adding Git-Bash to the new Windows Terminal
markdown
在 Terminal 按下 `ctrl` + `,`,會開啟 `settings.json`
```
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"defaultProfile": "{00000000-0000-0000-ba54-000000000001}",
"profiles":
{
"defaults":
{
// Put settings here that you want to apply to all profiles
},
"list":
[
// put one of the configuration below right here
]
}
}
```
在 list 的區段中加入,依據正確git安裝路徑取消`commandline`和`icon`的註解
```
{
"guid": "{00000000-0000-0000-ba54-000000000002}",
//"commandline": "%PROGRAMFILES%/git/usr/bin/bash.exe -i -l",
//"commandline": "%USERPROFILE%/AppData/Local/Programs/Git/git-bash.exe -l -i",
"commandline": "%USERPROFILE%/AppData/Local/Programs/Git/bin/sh.exe --login",
//"commandline": "%USERPROFILE%/AppData/Local/Programs/Git/bin/bash.exe -l -i",
//"commandline": "%USERPROFILE%/scoop/apps/git/current/usr/bin/bash.exe -l -i",
//"icon": "%PROGRAMFILES%/Git/mingw64/share/git/git-for-windows.ico",
"icon": "%USERPROFILE%/AppData/Local/Programs/Git/mingw64/share/git/git-for-windows.ico",
// "icon": "%USERPROFILE%/apps/git/current/usr/share/git/git-for-windows.ico",
"name": "Bash",
"startingDirectory": "%USERPROFILE%"
}
```
# Reference
# [Adding Git-Bash to the new Windows Terminal](https://stackoverflow.com/questions/56839307/adding-git-bash-to-the-new-windows-terminal)
2020-10-20
新的 Windows Terminal 視窗分割
markdown
# New Windows Terminal
Windows 終端機是一種現代化、快速、高效、功能強大且具生產力的終端應用程式,適合命令列工具和 Shell (像是命令提示字元、PowerShell 和 WSL) 的使用者。主要功能包括多個索引標籤、窗格、Unicode 和 UTF-8 字元支援、GPU 加速的文字呈現引擎,以及自訂主題、樣式和設定。
可以直接在 Window 10 的 Microsoft Store 下載。
* 下載安裝[Windows Terminal](https://www.microsoft.com/zh-tw/p/windows-terminal/9n0dx20hk701?activetab=pivot:overviewtab)
* [Source Code](https://github.com/microsoft/terminal)
# 視窗分割
* 自動分割視窗:Alt + Shift + D
* 建立一個新的水平分割:Alt + Shift + -(英文鍵上排的減號)
* 建立一個新的垂直分割:Alt + Shift + +(英文鍵上排的加號)
* 移動遊標至其它視窗:Alt + Left, Alt + Right, Alt + Up, Alt + Down
* 調整遊標所在視窗的大小:Alt + Shift + Left, Alt + Shift + Right, Alt + Shift + Up, Alt + Shift + Down
## 或是 Ctrl + Shift + P 開啟輸入命令名稱的快捷輸入框
1. 選擇 Split Pane...
2. 選擇要開啟的Shell環境「Azure Cloud Shell、Windows PowerShell、命令提示字元、或是自己安裝的 WSL 子系統(Linux)」
3. 選擇要分割的視窗方向,「自動,水平、垂直」
* 自動:依視窗大小自動做水平或垂直分割
* 水平:分割成上和下
* 垂直:分割成左和右
# 關閉分割
* 將遊標所在的視窗關閉:Ctrl + Shift + W
# 透過指令啟動一個視窗,裡面已經分割成三個
```
wt -p "Command Prompt" `; split-pane -p "Windows PowerShell" `; split-pane -H wsl.exe
```
# 參考
* [Using command-line arguments for Windows Terminal](https://github.com/MicrosoftDocs/terminal/blob/master/TerminalDocs/command-line-arguments.md)
* [The New Windows Terminal Is Ready; Here’s Why It’s Amazing](https://www.howtogeek.com/673729/heres-why-the-new-windows-10-terminal-is-amazing/) - Chris Hoffman, May 19, 2020
2020-06-17
查 Windows Port 被哪一個程式使用
markdown
查 Windows Port 被哪一個程式使用
```
netstat -ano | findstr "0.0.0.0:443"
```
```
tasklist|findstr "4836"
```
VMware Workstation and Hyper-V are not compatible.
markdown
當 Windows 作業系統啟用 Hyper-V 時,會無法使用 VMware Workstation。所以會出現以下的訊息
> VMware Workstation and Hyper-V are not compatible. Remove the Hyper-V role from the system before running VMware Workstation.
Disable Hyper-V
1. Open cmd in admin mode
2. bcdedit /set hypervisorlaunchtype off
3. restart!?
To enable the Hyper-V role again use the following command:
1. Open cmd in admin mode
2. bcdedit /set hypervisorlaunchtype auto
3. restart!?
2020-06-04
Jupyter Notebooks 使用 .NET C# 進行交互開發
markdown
# 安裝步驟
* 安裝好 .NET Core 3.1 SDK
* 安裝好 Python 3 (預設已裝 pip)
* 安裝 Jupyter
```
pip install jupyter
```
* 確認 Jupyter 安裝是否正確
```
jupyter kernelspec list
```
* 安裝 .NET Interactive
方法1.
```
dotnet tool install -g --add-source "https://dotnet.myget.org/F/dotnet-try/api/v3/index.json" Microsoft.dotnet-interactive
dotnet interactive jupyter install
```
方法2.
>請注意:如果此前已經安裝了dotnet try全局工具,需要先卸載舊版本的軟體再安裝。
```
dotnet tool install --global dotnet-try
```
在Anaconda提示符下通過命令安裝.NET內核:
```
dotnet try jupyter install
```
* 再次使用 `jupyter kernelspec list` 命令檢查安裝好的 .NET 版本 Jupyter 支援
顯示結果
```
.net-csharp ~\jupyter\kernels\.net-csharp
.net-fsharp ~\jupyter\kernels\.net-fsharp
.net-powershell ~\jupyter\kernels\.net-powershell
python3 ~\jupyter\kernels\python3
```
# 使用 Jupyter Notebooks
```
mkdir yourapp
cd yourapp
jupyter notebook
```
然後就可以在瀏覽器使用 jupyter 了。
開啟 jupyter 的右側可以新增 「NET(C#)、NET (F#)、NET (PowerShell)、Python 3」
## 功能介紹
### HTML解析和輸出
```
display(HTML("Hello, alger!"));
```
### 使用 JavaScript
```
Javascript(@"alert(""Hello, Alger!"")");
```
### 使用 pocketView
```
display(
span(
img[src:"https://www.google.com/favicon.ico",style:"height:4.5em"],
a[href: @"http://www.google.com", target: "blank", style: "color:green"](b("諸事不宜"))
)
);
```
### 使用 markdown
```
%%markdown
* 一
* 二
* 三
| one | two |
|-----|-----|
| 一 | 二 |
| 臺 | 貮 |
```
### 類庫導入
可以支持對 C# 類庫的導入,使用 #r語法,從 nuget 導入程式庫
```
#r "nuget:[,]"
```
例如:
```
#r "nuget:System.Reactive.Linq, 4.1.5"
```
導入程式庫時需要等待。
### 對像格式化
情況下,.NET Notebook使用戶能夠以表格式顯示有關對象的有用信息。比如對一個Ienumerable對象display顯示如下:
```
display(new[]{"hello", "world"});
Enumerable.Range(1,5);
```
### Formatter API
### 繪圖
.net notebook的繪圖功能都使用XPlot.Plotly 可視化包。
```
//Install XPlot package
#r "nuget:XPlot.Plotly"
//Plotting functionalities
using XPlot.Plotly;
var chart = Chart.Plot(
new Graph.Scattergl()
{
x = housingData["logitude"],
y = housingData["latitude"],
mode = "markers",
marker = new Graph.Marker()
{
color = housingData["median_house_value"],
colorscale = "Jet"
}
}
);
chart.Width = 600;
chart.Height = 600;
display(chart);
```
NET (F#)、NET (PowerShell)、Python 3」
訂閱:
文章 (Atom)