还将运行已完成的 Course Manager 应用程序。

保存对对象所做的更改

  1. Update

  2. saveChanges 控件。

    saveChanges_Click 事件处理程序方法。

  3. 粘贴以下代码,这些代码将对象更改保存到数据库中。

    VB
    C#
    C++
    F#
    JScript
    and refresh the form. schoolContext.SaveChanges() MessageBox.Show( "Changes saved to the database.") Me.Refresh() Catch ex As Exception MessageBox.Show(ex.Message) EndTry
    Try    ' Save object changes to the database,     ' display a message, and refresh the form.
        schoolContext.SaveChanges()
        MessageBox.Show(
                        "Changes saved to the database.")
        
                        Me.Refresh()
    
                        Catch ex 
                        As Exception
        MessageBox.Show(ex.Message)
    
                        EndTry
    try { // Save object changes to the database, // display a message, and refresh the form. schoolContext.SaveChanges(); MessageBox.Show( "Changes saved to the database."); this.Refresh(); } catch(Exception ex) { MessageBox.Show(ex.Message); }
    try
    {
        
                        // Save object changes to the database, // display a message, and refresh the form.
        schoolContext.SaveChanges();
        MessageBox.Show(
                        "Changes saved to the database.");
        
                        this.Refresh();
    }
    
                        catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    
    
                      

通过释放长时间运行的对象上下文关闭连接

  • 此代码在关闭窗体之前释放对象上下文。

    VB
    C#
    C++
    F#
    JScript
    object context. schoolContext.Dispose()
    ' Dispose the object context.
    schoolContext.Dispose()
    
    
                      
    //Dispose the object context. schoolContext.Dispose();
    //Dispose the object context.
    schoolContext.Dispose();
    
    
                      

生成并运行类计划应用程序

  1. “开始执行(不调试)”

    此时将生成并启动应用程序。

  2. ComboBox 控件中选择一个系。

    此时将显示属于该系的课程。

  3. Update

    此时会将更改保存到数据库中,并显示一个消息框,指示已保存了更改。

后续步骤

您还完成了这一实体框架快速入门。

请参见

概念

其他资源

相关文章: