Ado.Net Entity Framework – Entity Model ile Windows Form Uygulamaları ve Bug

Veri tabanı ile ilişkisel olarak yapmış olduğumuz işlemleri Ado.Net Entity Framework ile yaptığımızda çok daha hızlı ve kolay bir şekilde yapabilinmesi mümkündür. Ne kadar basit yapılabileceğine ilişkin basit bir örnek verelim.

Visual Studio 2008 ile Windows From projesi oluşturuyor ve sonrasında entity data model template ‘ini projeye ekliyoruz. edmx ‘in içerisine gerekli olan tabloları ekledikten sonra işlemleri yapmaya başlayabiliriz.

Windows Formun üzerine aşadağıdaki kontrolleri ekliyoruz.

Windows Form

Tasarımı oluşturduktan sonra aşağıdaki kodları ekliyoruz.

using System;
using System.Windows.Forms;
using System.Data.Objects;
using System.Data.Objects.DataClasses;

namespace CourseManager
{
    public partial class CourseViewer : Form
    {
        public CourseViewer()
        {
            InitializeComponent();
        }
        private SchoolEntities schoolContext;
        private void closeForm_Click(object sender, EventArgs e)
        {
            schoolContext.Dispose();
            this.Close();
        }

        private void CourseViewer_Load(object sender, EventArgs e)
        {
            schoolContext = new SchoolEntities();
            ObjectQuery<Department> departmentQuery = schoolContext.Department.Include("Course").OrderBy("it.Name");
            try
            {
                this.departmentList.DisplayMember = "Name";
                this.departmentList.DataSource = departmentQuery;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void departmentList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Department department = (Department)this.departmentList.SelectedItem;
                courseGridView.DataSource = department.Course;
                courseGridView.Columns["Department"].Visible = false;
                courseGridView.Columns["CourseGrade"].Visible = false;
                courseGridView.Columns["OnlineCourse"].Visible = false;
                courseGridView.Columns["OnSiteCourse"].Visible = false;
                courseGridView.Columns["Person"].Visible = false;

                courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void saveChanges_Click(object sender, EventArgs e)
        {
            try
            {
                schoolContext.SaveChanges();
                MessageBox.Show("Changes saved to the database");
                this.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Yukarıdaki kod bloglarını da projeye ekledikten sonra projemizi çalıştırdığımız aşağıdaki gibi bir görünüme sahip olacağız.

Debugging Windows Form

Evet herşey güzel gözüküyor.

Peki, bug nerede?

            {
                Department department = (Department)this.departmentList.SelectedItem;
                courseGridView.DataSource = department.Course;
                courseGridView.Columns["Department"].Visible = false;
                courseGridView.Columns["CourseGrade"].Visible = false;
                courseGridView.Columns["OnlineCourse"].Visible = false;
                courseGridView.Columns["OnSiteCourse"].Visible = false;
                courseGridView.Columns["Person"].Visible = false;

                courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
Veri tabanında yer alan tabloların gözükmemesi için görünebilirliğini pasif yapıyoruz. Ancak bu işlem esnasında yanlışlıkla CourseGrade yerine CourseGrades yazdım ve karşımıza aşağıdaki gibi bir sonuç çıkmıştır.

EntityBug

Dikkat ederseniz yanlış yazdığımız bölüm sonrası kapatmış olduğumuz bütün tablo isimleri gözükmüştür.

Ancak her ne olursa olsun Entity Framework ‘ün uygulamalarımıza kattıkları oldukça fazladır. Bu tür Bug ‘lar da .Net Framework ‘ün tamamlanması ile büyük ihtimal ortadan kalkacaktır.

Eğer ki sizler de bu tür buglar keşkefederseniz www.connect.microsoft.com adresinden hata merkezine belirtebilirsiniz.

Umarım yararlı olabilmiştir.

Yorum Gönder

0 Yorumlar

Ad Code

Responsive Advertisement