星期日, 3月 31, 2019

[EF] 使用 Enum

練習在 EF 的 edmx 上使用 Enum

[EF] 使用 Enum-0

  • 轉換為 Enum
[EF] 使用 Enum-1

[EF] 使用 Enum-2

星期五, 3月 29, 2019

[git] amend 參數

之前假如 commit 完後,又發現忘記 commit 進去的內容,通常都會下 git reset 來幹掉 commit,再重新 commit,該作法最大缺點就是 commit message 要馬就重打,要不就是 git reset 前要先複製下來,後來學會 git commit --amend 後,總算是不用再去理會 commit message 啦
git commit --amend -m "commit message"
-m 參數導致兩種用法
  1. 有 -m 參數:表示只修改 commit message 而已
  2. 沒有 -m 參數:表示會把 index 內內容放進最後 commit 內,並跳出視窗確認 commit message 是否要修改,當然視窗內就會有最後一個 commit 的 commit message
--amend 只能針對最後一個 commit 來變化喔

Team Explorer

Git- amend-1

Tortoisegit

Git- amend-2

星期三, 3月 27, 2019

[Win] 錯誤事件 4321

收到 Windows 錯誤訊息通知,詢問同事才發現,原來公司要導入 VMWare ,現在進行移轉測試,因為電腦名稱衝突所產生的錯誤訊息,測試機改了 IP 卻忘了改電腦名稱,測試機上發出來的訊息。

mail 出來的錯誤訊息

錯誤事件 4321-1

Windows 事件檢視器內錯誤訊息

錯誤事件 4321-2

錯誤事件 4321-3

星期六, 3月 16, 2019

[EF] Store Procedure

練習在 EF 的 edm 上使用 Store Procedure

把 Store Procedure 匯入


匯入後在 DbContext 內可以看見匯入自動產生的 Code
namespace EFStoreProcedure
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Core.Objects;
    using System.Linq;
    
    public partial class EFDbContext : DbContext
    {
        public EFDbContext()
            : base("name=EFDbContext")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<Employ> Employ { get; set; }
    
        public virtual int uspEmploy_Insert(string empNO, string name, Nullable<System.DateTime> hireDate)
        {
            var empNOParameter = empNO != null ?
                new ObjectParameter("EmpNO", empNO) :
                new ObjectParameter("EmpNO", typeof(string));
    
            var nameParameter = name != null ?
                new ObjectParameter("Name", name) :
                new ObjectParameter("Name", typeof(string));
    
            var hireDateParameter = hireDate.HasValue ?
                new ObjectParameter("HireDate", hireDate) :
                new ObjectParameter("HireDate", typeof(System.DateTime));
    
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("uspEmploy_Insert", empNOParameter, nameParameter, hireDateParameter);
        }
    
        public virtual ObjectResult<uspEmploy_Select_Result> uspEmploy_Select()
        {
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<uspEmploy_Select_Result>("uspEmploy_Select");
        }
    }
}

點選目標 Table 並點選 [預存程序對應],在這邊設定 insert、update 和 delete 對應的 Store Procedure


只針對 insert 進行設定,選擇 uspEmploy_Insert


在 Console 內執行並觀察結果
namespace EFStoreProcedure
{
    class Program
    {
        static void Main(string[] args)
        {
            EFDbContext context = new EFDbContext();
            context.Employ.Add(new Employ { EmpNO = "10801", Name = "張三", HireDate = new DateTime(2019, 3, 16) });
            context.SaveChanges();

            var data = context.uspEmploy_Select();
            foreach (var item in data)
            {
                Console.WriteLine($"{item.EmpNO}-{item.Name}-{item.HireDate}");
            }

        }
    }
}
執行觀察結果


透過 SQL Profile 來觀察是否使用 Store Procedure



加入 Store Procedure 時,預設是會勾選 [將選取的預存程序和函數匯入實體模型],匯入 uspEmploy_Delete 時故意取消


在模型瀏覽器內就可以發現,uspEmploy_Delete 只出現在 Store 內


點選 [加入函式匯入]


把 uspEmploy_Delete 加入,在這邊是可以進行更名,沒有一定要跟 Store 內名稱一樣


這樣 uspEmploy_Delete 就會出現在兩邊



星期四, 3月 07, 2019

[SQL] 自我參考 Foreign Key

以往使用 Foreign Key 都是在不同 Table 之間設定,看 EF 影片教學時才意識到,Foregin Key 也可以設在自身
CREATE TABLE [dbo].[MyEmployees]
(
  [EmployeeID] [smallint] NOT NULL,
  [FirstName] [nvarchar](30) NOT NULL,
  [LastName] [nvarchar](40) NOT NULL,
  [Title] [nvarchar](50) NOT NULL,
  [DeptID] [smallint] NOT NULL,
  [ManagerID] [smallint] NULL,
  CONSTRAINT [PK_EmployeeID] PRIMARY KEY CLUSTERED 
  (
    [EmployeeID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[MyEmployees]  WITH CHECK ADD  CONSTRAINT [FK_MyEmployees_MyEmployees1] FOREIGN KEY([ManagerID])
REFERENCES [dbo].[MyEmployees] ([EmployeeID])
GO

ALTER TABLE [dbo].[MyEmployees] CHECK CONSTRAINT [FK_MyEmployees_MyEmployees1]
GO
FK GUI 設定畫面

[SQL] 自我參考 Foreign Key-1

SSMS 圖表

[SQL] 自我參考 Foreign Key-2

星期二, 3月 05, 2019

[EF] 使用 View

公司有個供應商相關邏輯是,供應商會有很多聯絡人,但必須設定一個主要聯絡人,也因此 Supplier Table 理論上應該有主要聯絡人相關資料,同事提出該盲點來討論後是利用 LINQ 來解決,那我就練習一下在 Entity Framework 使用 View 來 try 看看囉

完整情境為一個儀器會有多個儀器供應商,使用者需求是要在畫面顯示供應商主要聯絡人相關聯絡資訊

[EF] 使用 View-0