Advertisement
wingman007

EntityFrameworkGettingStarted

Nov 9th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. using System.Data.Objects;
  12. using System.Data.Objects.DataClasses;
  13. // using SchoolModel;
  14.  
  15. // Quiick Start Tutorial http://msdn.microsoft.com/en-us/library/bb399182(v=vs.100).aspx
  16. // Example from http://msdn.microsoft.com/en-us/library/bb399568(v=vs.100).aspx
  17.  
  18. namespace CourseManager
  19. {
  20.     public partial class CourseViewer : Form
  21.     {
  22.         // Create an ObjectContext instance based on SchoolEntity.
  23.         private SchoolEntities schoolContext;
  24.  
  25.         public CourseViewer()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.  
  30.         private void closeForm_Click(object sender, EventArgs e)
  31.         {
  32.             //Dispose the object context.
  33.             schoolContext.Dispose();
  34.  
  35.             // Close the form.
  36.             this.Close();
  37.         }
  38.  
  39.         private void CourseViewer_Load(object sender, EventArgs e)
  40.         {
  41.             /*
  42.             // Initialize the ObjectContext.
  43.             schoolContext = new SchoolEntities();
  44.  
  45.             // Define a query that returns all Department objects and related
  46.             // Course objects, ordered by name.
  47.             ObjectQuery<Department> departmentQuery =
  48.                 schoolContext.Departments.Include("Courses").OrderBy("it.Name"); //.Include("Courses").OrderBy("it.Name");
  49.             try
  50.             {
  51.                 // Bind the ComboBox control to the query, which is
  52.                 // executed during data binding.
  53.                 this.departmentList.DisplayMember = "Name";
  54.                 this.departmentList.DataSource = departmentQuery;
  55.  
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 MessageBox.Show(ex.Message);
  60.             }
  61.             */
  62.             //Initialize the ObjectContext
  63.             schoolContext = new SchoolEntities();
  64.  
  65.             // Define a query that returns all Department  
  66.             // objects and course objects, ordered by name.
  67.             var departmentQuery = from d in schoolContext.Departments.Include("Courses")
  68.                                   orderby d.Name
  69.                                   select d;
  70.             try
  71.             {
  72.                 // Bind the ComboBox control to the query,
  73.                 // which is executed during data binding.
  74.                 // To prevent the query from being executed multiple times during binding,
  75.                 // it is recommended to bind controls to the result of the Execute method.
  76. //-                this.departmentList.DisplayMember = "Name";
  77. //-                this.departmentList.DataSource = ((ObjectQuery)departmentQuery).Execute(MergeOption.AppendOnly);
  78.  
  79.                 this.departmentList.DataSource = departmentQuery.ToList();
  80.                 courseGridView.DataSource = departmentQuery.ToList();
  81.  
  82. //                this.departmentList.DataSource = departmentQuery.ToList();
  83.                 courseGridView.DataSource = departmentQuery.ToList();
  84.             }
  85.             catch (Exception ex)
  86.             {
  87.                 MessageBox.Show(ex.Message);
  88.             }
  89.         }
  90.  
  91.         private void departmentList_SelectedIndexChanged(object sender, EventArgs e)
  92.         {
  93.             /*
  94.             try
  95.             {
  96.                 // Get the object for the selected department.
  97.                 Department department =
  98.                 (Department)this.departmentList.SelectedItem;
  99.  
  100.                 // Bind the grid view to the collection of Course objects
  101.                 // that are related to the selected Department object.
  102.                 courseGridView.DataSource = department.Courses;
  103.  
  104.                 // Hide the columns that are bound to the navigation properties on Course.
  105.                 courseGridView.Columns["Department"].Visible = false;
  106.                 courseGridView.Columns["CourseGrades"].Visible = false;
  107.                 courseGridView.Columns["OnlineCourse"].Visible = false;
  108.                 courseGridView.Columns["OnsiteCourse"].Visible = false;
  109.                 courseGridView.Columns["People"].Visible = false;
  110.  
  111.                 courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
  112.             }
  113.             catch (Exception ex)
  114.             {
  115.                 MessageBox.Show(ex.Message);
  116.             }
  117.             */
  118.  
  119.             try
  120.             {
  121.                 //Get the object for the selected department.
  122.                 Department department = (Department)this.departmentList.SelectedItem;
  123.  
  124.                 //Bind the grid view to the collection of Course objects
  125.                 // that are related to the selected Department object.
  126. //-                courseGridView.DataSource = department.Courses;
  127.  
  128.                 courseGridView.DataSource = department.Courses.ToList();
  129.  
  130.                 // Hide the columns that are bound to the navigation properties on Course.
  131.                 courseGridView.Columns["Department"].Visible = false;
  132.                 courseGridView.Columns["StudentGrades"].Visible = false;
  133.                 courseGridView.Columns["OnlineCourse"].Visible = false;
  134.                 courseGridView.Columns["OnsiteCourse"].Visible = false;
  135.                 courseGridView.Columns["People"].Visible = false;
  136.                 courseGridView.Columns["DepartmentId"].Visible = false;
  137.  
  138.                 courseGridView.AllowUserToDeleteRows = false;
  139.                 courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
  140.             }
  141.             catch (Exception ex)
  142.             {
  143.                 MessageBox.Show(ex.Message);
  144.             }
  145.  
  146.         }
  147.  
  148.         private void saveChanges_Click(object sender, EventArgs e)
  149.         {
  150.             try
  151.             {
  152.                 // Save object changes to the database,
  153.                 // display a message, and refresh the form.
  154.                 schoolContext.SaveChanges();
  155.                 MessageBox.Show("Changes saved to the database.");
  156.                 this.Refresh();
  157.             }
  158.             catch (Exception ex)
  159.             {
  160.                 MessageBox.Show(ex.Message);
  161.             }
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement