ADO.NET Entity Framework查询实体和关联(实体框架快速入门)

在本任务中,您将针对在 School 模型中表示实体和关联的 CLR 对象创建强类型查询,并将显示控件绑定到从这些查询中返回的对象集合。

在 School 数据库中查询系

  1. CourseViewer 窗体的代码文件的开始处,添加以下 using (C#) 或 Imports (Visual Basic) 语句,以引用从 School 数据库和实体命名空间中创建的模型。

    using System.Data.Objects;

                        using System.Data.Objects.DataClasses;
     
  2. CourseViewer 窗体的分部类定义的顶部,添加以下用于创建 ObjectContext 实例的代码。

    //Create an ObjectContext instance based on SchoolEntityprivate         SchoolEntities schoolContext;
  3. CourseViewer 窗体设计器中,双击 CourseViewer 窗体。
  4. 此时将打开窗体的代码页并创建 courseViewer _Load 事件处理程序方法。

  5. courseViewer _Load 事件处理程序方法中,复制并粘贴以下用于定义 DataGridView 的代码,执行返回系科集合的查询(按 Name 排序),然后将 Department 对象的集合绑定到 departmentList 控件。

    //Initialize the ObjectContext
    schoolContext = new SchoolEntities();


                        // Define a query that returns all Department
     // objects and course objects, ordered by name.var departmentQuery =
                        from d
                        in schoolContext.Departments.Include(
                        "Courses")
                         
                        orderby d.Name
                         
                        select d;

                        try
    {
       
                        // Bind the ComboBox control to the query, // which is executed during data binding.this.departmentList.DisplayMember =
                        "Name";
       
                        this.departmentList.DataSource = departmentQuery;
    }

                        catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }


                     
     
        
     
        
     
        
     
        
     
        
     
        

显示所选系的课程

  1. CourseViewer 窗体设计器中,双击 departmentList 控件。

    此时将创建 departmentList_SelectedIndexChanged 事件处理程序方法。

  2. 粘贴以下用于加载与所选系相关的课程的代码。

    try
                {
                   
                        //Get the object for the selected department.
                    Department department = (Department)
                        this.departmentList.SelectedItem;

                   
                        //Bind the grid view to the collection of Course objects// that are related to the selected Department object.
                    courseGridView.DataSource = department.Courses;

                   
                        // Hide the columns that are bound to the navigation properties on Course.
                    courseGridView.Columns[
                        "Department"].Visible =
                        false;
                    courseGridView.Columns[
                        "StudentGrades"].Visible =
                        false;
                    courseGridView.Columns[
                        "OnlineCourse"].Visible =
                        false;
                    courseGridView.Columns[
                        "OnsiteCourse"].Visible =
                        false;
                    courseGridView.Columns[
                        "People"].Visible =
                        false;
                    courseGridView.Columns[
                        "DepartmentId"].Visible =
                        false;

                    courseGridView.AllowUserToDeleteRows =
                        false;
            courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                }
               
                        catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    后续步骤

您已成功地创建了返回 DepartmentCourse 对象的查询并将这些对象绑定到控件。 接下来,您需要将在数据网格中对 Course 对象所做的更改保存到数据库中: 插入和更新数据(实体框架快速入门)

请参见
概念
其他资源

转载于:https://www.cnblogs.com/bmate/archive/2010/12/29/1920433.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值