星期五, 2月 22, 2019

[EF] 簡單 CRUD 練習

看完該課程 - CRUD C#.Net with Entity Framework 後的簡易練習筆記

[EF] 簡單 CRUD 練習-1
C# Code
using System.Data.Entity;
using SimpleCRUD.Models;

namespace SimpleCRUD
{
    public partial class Form1 : Form
    {
        CRUDContext context = new CRUDContext();
        Employee employ = new Employee();

        public Form1()
        {
            InitializeComponent();
            DataControlVisible();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ControlClear();
            DgvSource();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // DBContext 物件會佔用資源,因此必須特別注意其生命週期,
            // 通常都會利用 using 確保會釋放佔用資源,在這是 Form 結束時,
            // 手動進行釋放,要不然就只能等待 GC 進行資源回收

            // https://docs.microsoft.com/zh-tw/ef/ef6/fundamentals/databinding/winforms
            // 根據該文章釋放 Context 是在 Closing Event 中手動進行

            context.Dispose();
        }

        private void ControlClear()
        {
            txtName.Text = string.Empty;
            cboDepartment.Text = "資訊";
            dtHireDate.Value = DateTime.Today;
        }

        private void ControlBinding()
        {
            txtName.Text = employ.Name;
            cboDepartment.Text = employ.Department.Trim();
            dtHireDate.Value = employ.HireDate;
        }

        private void DataControlVisible()
        {
            var state = context.Entry(employ).State;
            if (state == EntityState.Added || state == EntityState.Modified)
            {
                BtnAdd.Visible = false;
                BtnModify.Visible = false;
                BtnDelete.Visible = false;
                BtnSave.Visible = true;
                BtnRevert.Visible = true;
            }
            else
            {
                BtnAdd.Visible = true;
                BtnModify.Visible = true;
                BtnDelete.Visible = true;
                BtnSave.Visible = false;
                BtnRevert.Visible = false;
            }
        }

        private void DgvSource()
        {
            DgvSearch.DataSource = context.Employee.ToList();
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            ControlClear();

            employ = new Employee
            {
                Name = txtName.Text,
                Department = cboDepartment.Text,
                HireDate = dtHireDate.Value
            };

            // 下述兩種方式都可以透過 SaveChange() 把資料存進 DB 內
            // 方法一:把資料新增進資料集
            context.Employee.Add(employ);
            // 方法二:把資料狀態設為 EntityState.Added
            // context.Entry(employ).State = EntityState.Added;

            DataControlVisible();
        }

        private void BtnModify_Click(object sender, EventArgs e)
        {
            context.Entry(employ).State = EntityState.Modified;
            employ.Name = "取消測試";
            ControlBinding();
            DataControlVisible();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            employ.Name = txtName.Text;
            employ.Department = cboDepartment.Text;
            employ.HireDate = dtHireDate.Value;

            context.SaveChanges();
            DgvSource();
            DataControlVisible();
        }

        private void BtnRevert_Click(object sender, EventArgs e)
        {
            // 只針對該筆資料,重新讀取
            context.Entry(employ).Reload();
            ControlBinding();
            DataControlVisible();
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否刪除該筆資料", "刪除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }

            context.Employee.Remove(employ);
            context.SaveChanges();
            employ = null;
            ControlClear();
            DgvSource();
        }

        private void DgvSearch_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (DgvSearch.CurrentRow == null)
            {
                return;
            }
            
            int ID = Convert.ToInt32(DgvSearch.CurrentRow.Cells["ID"].Value);
            employ = context.Employee.Single(s => s.ID == ID);
            ControlBinding();
        }
    }
}

[EF] 簡單 CRUD 練習-2

沒有留言:

張貼留言