EntityDataSource.ContextCreating 事件


發生於 EntityDataSource 建立用來搭配實體資料物件運作的 ObjectContext 時。Occurs when the EntityDataSource creates the ObjectContext that is used to work with entity data objects.

 event EventHandler<System::Web::UI::WebControls::EntityDataSourceContextCreatingEventArgs ^> ^ ContextCreating;
public event EventHandler<System.Web.UI.WebControls.EntityDataSourceContextCreatingEventArgs> ContextCreating;
member this.ContextCreating : EventHandler<System.Web.UI.WebControls.EntityDataSourceContextCreatingEventArgs> 
Public Custom Event ContextCreating As EventHandler(Of EntityDataSourceContextCreatingEventArgs) 




下列範例顯示如何建立 ObjectContext 物件的變數 Page ,並將它指派給 Context 物件的屬性 EntityDataSourceContextCreatingEventArgsThe following example shows how to create an ObjectContext variable of the Page object and assign it to the Context property of the EntityDataSourceContextCreatingEventArgs object.

public partial class _Default : System.Web.UI.Page
        AdventureWorksModel.AdventureWorksEntities objCtx =
            new AdventureWorksModel.AdventureWorksEntities();

        protected void EntityDataSource2_ContextCreating(object sender,
            EntityDataSourceContextCreatingEventArgs e)
            e.Context = objCtx;


處理 ContextCreating 事件以提供您自己的專案, ObjectContext 而不是 ObjectContext 建立新的。Handle the ContextCreating event to supply your own ObjectContext instead of having a new ObjectContext created. 如果 ContextCreating 未處理事件,或處理常式未提供 ObjectContext ,則會 EntityDataSource 根據其設定建立一個事件。If the ContextCreating event is not handled or the handler does not provide an ObjectContext, the EntityDataSource creates one according to its configuration. ObjectContext 提供給 ContextCreating 事件時,會將相同的實例指派給 Context 後續事件之事件物件的屬性。When an ObjectContext is provided to the ContextCreating event, the same instance is assigned to the Context property of the event object of successive events. 如需詳細資訊,請參閱 Life-Cycle 管理的物件內容 (EntityDataSource) For more information, see Object Context Life-Cycle Management (EntityDataSource). 如需有關物件內容的詳細資訊,請參閱 識別解析、狀態管理及變更追蹤For more information about the object context, see Identity Resolution, State Management, and Change Tracking.