HOW TO:將控制項繫結到衍生型別 (Entity Framework)
Entity Framework 可讓您將 Windows Forms 控制項 (例如 ComboBox 或 DataGridView) 繫結至 EntityCollection。 然而,如果對 EntityCollection 執行 OfType 方法以傳回衍生型別物件的集合,則不能將傳回的 IEnumerable 直接繫結至控制項。 本主題中的範例示範如何使用 CreateSourceQuery 方法建立可定義 EntityCollection 的 ObjectQuery。 這個查詢以 OfType 方法執行,可以繫結至特定的子型別。 如需詳細資訊,請參閱將物件與控制項繫結 (Entity Framework)。
本主題中的範例以修改過的 School 模型版本為依據。 這個版本支援以 Course 為抽象型別的一類一表 (Table-Per-Type) 繼承。 請完成對應繼承:一類一表逐步解說,將 School 模型修改為支援本主題中使用的一類一表繼承範例。
範例
下列範例是來自 Windows Form。 表單載入時,Department 物件的 ObjectResult 會藉由呼叫 ObjectQuery 的 Execute 方法傳回。 這個結果會繫結到下拉式方塊。 選取某個訂單時,會在 Course 物件的相關 EntityCollection 呼叫 CreateSourceQuery 方法。 傳回的 ObjectQuery 是使用 OfType 方法執行的,而其結果則繫結至 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();
}
}
}
另請參閱
工作
HOW TO:將物件繫結到 Windows Presentation Foundation 控制項 (Entity Framework)
HOW TO:將物件繫結到 Windows Form 控制項 (Entity Framework)