使用实体框架 可以将 Windows 窗体控件(如 ComboBox 或 DataGridView)绑定到 EntityCollection。 不过,在执行 EntityCollection 的 OfType 方法以返回派生类型的对象集合时,不能直接将返回的 IEnumerable 绑定到控件。 本主题中的示例显示如何使用 CreateSourceQuery 方法创建定义 EntityCollection 的 ObjectQuery。 使用 OfType 方法执行此查询,以便对特定子类型执行绑定。 有关更多信息,请参见将对象绑定到控件(实体框架)。
本主题中的示例基于 School 模型的修改版本。 此版本支持每种类型一个表继承,并使用 Course 作为抽象类型。 完成映射继承 - 每种类型一个表演练,将 School 模型修改为支持本主题中使用的每种类型一个表继承的示例。
示例
以下示例来自 Windows 窗体。 在加载窗体时,会通过调用 ObjectQuery 的 Execute 方法来返回 Department 对象的 ObjectResult。 此结果被绑定到一个组合框。 在选择某个订单时,将对 Course 对象的相关 EntityCollection 调用 CreateSourceQuery 方法。 使用 OfType 方法执行返回的 ObjectQuery,并将结果绑定到 DataGridView 控件。 与 OfType 方法一起使用的类型是根据窗体中单选框的设置来设置的。
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
namespace Microsoft.Samples.Edm
{
public partial class SchoolForm : Form
{
private SchoolEntities context;
private bool isOnlineCourse = false;
public SchoolForm()
{
// Initializes the designer-generated controls.
InitializeComponent();
if (isOnlineCourse) radioButtonOnline.Checked = true;
else radioButtonOnsite.Checked = true;
}
private void SchoolForm_Load(object sender, EventArgs e)
{
// Initialize the object context.
try
{
context = new SchoolEntities();
// Create a query for all departments.
ObjectQuery<Department> departmentQuery =
context.Departments;
// Display the department name in the combo box.
this.comboBoxDepartment.DisplayMember = "Name";
// Bind the combo box to the ObjectResult of the departments
// that are returned when the query is executed.
this.comboBoxDepartment.DataSource =
departmentQuery.Execute(MergeOption.AppendOnly);
}
catch (EntitySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void BindToCourseByType()
{
// Get the currently selected Department object.
Department selectedDepartment =
(Department)this.comboBoxDepartment.SelectedItem;
try
{
if (isOnlineCourse)
{
// Bind the data grid to the result of the execution of the ObjectQuery
// that returns only the online courses for the selected department.
dataGridViewCourses.DataSource =
selectedDepartment.Courses.CreateSourceQuery()
.OfType<OnlineCourse>().Execute(MergeOption.AppendOnly);
}
else
{
// Bind the data grid to the result of the execution of the ObjectQuery
// that returns only the on-site courses for the selected department.
dataGridViewCourses.DataSource =
selectedDepartment.Courses.CreateSourceQuery()
.OfType<OnsiteCourse>().Execute(MergeOption.AppendOnly);
}
// Hide the columns bound to navigation properties.
dataGridViewCourses.Columns["Department"].Visible = false;
dataGridViewCourses.Columns["StudentGrades"].Visible = false;
dataGridViewCourses.Columns["People"].Visible = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void departmentComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
this.BindToCourseByType();
}
private void onlineRadio_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButtonOnline.Checked)
{
isOnlineCourse = true;
this.BindToCourseByType();
}
else
{
isOnlineCourse = false;
this.BindToCourseByType();
}
}
private void Close_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
另请参见
任务
如何:将对象绑定到 Windows Presentation Foundation 控件(实体框架)
如何:将对象绑定到 Windows 窗体控件(实体框架)