Xem mẫu

570 Part V Managing Data . 7 . . If you are using Visual C# 2010 Express Edition, perform the following tasks: . 7 .1 . . Click New Connection . If the Choose Data Source dialog box appears, in the Data source list box, click Microsoft SQL Server Database File . In the Data provider drop-down list box, se-lect .NET Framework Data Provider for SQL Server if it is not already selected and then click Continue . Note If you have already created database connections previously, this dialog box might not appear and the Connection Properties dialog box will be displayed . In this case, click the Change button adjacent to the Data source text box . The Change Data Source dialog box appears, which is the same as the Choose Data Source dialog box except that the Continue button has the legend OK instead . . 7 .2 . . In the Connection Properties dialog box, in the Database file name text click Browse . . 7 .3 . . In the Select SQL Server Database File dialog box, move to the folder C:\Program Files\Microsoft SQL Server\MSSQL10 .SQLEXPRESS\MSSQL\DATA, click the Northwind database file, and then click Open . . 7 .4 . . In the Log on to the server section of the dialog box, choose the Use Windows Authentication radio button and then click OK . . 8 . . On the Choose Your Data Connection page of the Entity Data Model Wizard, select the Save entity connection settings in App.Config as check box, type NorthwindEntities (this is the default name), and then click Next . If you are using Visual C# 2010 Express Edition, a message box appears asking whether you want to add the database file to your project . Click No . . 9 . . On the Choose Your Database Objects page, verify that the Pluralize or singularize gen-erated object names and Include foreign key columns in the model check boxes are both selected . In the Which database objects do you want to include in your model? list box, expand Tables and then click the Products (dbo) and Suppliers (dbo) tables . In the Model Namespace text box, type NorthwindModel (this is the default namespace) . The following image shows the completed page . Chapter 26 Displaying and Editing Data by Using the Entity Framework and Data Binding 571 . 10 . . Click Finish . The Entity Data Model Wizard generates entity classes called Supplier and Product based on the Suppliers and Products tables, with property fields for each column in the tables, as shown in the following image . The data model also defines navigation prop-erties that link the two entities together and maintain the relationship between them . In this case, a single Supplier entity can be related to many Product entities . You can modify the properties of an entity class by selecting the class and changing the property values in the Properties window . You can also use the Mapping Details pane that appears at the bottom of the window to select and edit the fields that appear in 572 Part V Managing Data an entity class . This is how you change the mapping from the logical properties in an entity to the physical columns in a table . Important This exercise assumes that you are using the default entity classes generated for the Suppliers and Products tables in the database, so please do not change anything! . 11 . . In Solution Explorer, expand the Northwind .edmx folder, and then double-click Northwind.designer.cs . Tip If Solution Explorer is not visible, on the View menu click Solution Explorer . The code generated by the Entity Data Model Wizard appears in the Code and Text Editor window . If you expand the Contexts region, you will see that it contains a class called NorthwindEntities that derives from the ObjectContext class . In the Entity Framework, the ObjectContext class performs a similar role to a DataContext class in LINQ to SQL, and you can use it to connect to the database . The NorthwindEntities class extends the ObjectContext class with logic to connect to the Northwind database, and to populate Supplier and Product entities (just like a custom DataContext class in LINQ to SQL) . The information concerning the connection you specified before creating the two entity classes is saved in an application configuration file . Storing the connection string in a configuration file enables you to modify the connection string without rebuilding the application; you simply edit the application configuration file . You’ll find it useful if you envisage ever needing to relocate or rename the database, or switching from using a local development database to a production database that has the same set of tables . The code for the two entity classes is located in the Entities region of the file . These entity classes are a little more complicated than the classes that you created manually in Chapter 25, but the general principles are similar . The additional complexity is the result of the entity classes indirectly implementing the INotifyPropertyChanging and INotifyPropertyChanged interfaces, and the navigational properties used to link related entities together . The INotifyPropertyChanging and INotifyPropertyChanged interfaces define events that the entity classes raise when their property values change . The vari-ous user interface controls in the WPF library subscribe to these events to detect any changes to data and ensure that the information displayed on a WPF form is up to date . Note The entity classes inherit from the System.Data.Objects.DataClasses.EntityObject class, which in turn inherits from the System.Data.Objects.DataClasses.StructuralObject class . The StructuralObject class implements the the INotifyPropertyChanging and INotifyPropertyChanged interfaces . Chapter 26 Displaying and Editing Data by Using the Entity Framework and Data Binding 573 Using .an .Application .Configuration .File An application configuration file provides a useful mechanism enabling a user to mod-ify some of the resources used by an application without rebuilding the application it-self . The connection string used for connecting to a database is an example of just such a resource . When you use the Entity Data Model Wizard to generate entity classes, a new file is added to your project called App .config . This is the source for the application configu-ration file, and it appears in the Solution Explorer window . You can examine the con-tents of the App .config file by double-clicking it . You will see that it is an XML file, as shown here (the text has been reformatted to fit on the printed page): The connection string is held in the element of the file . This string contains a set of elements in the form property=value . The elements are separated by a semi-colon character . The key properties are the Data Source, Initial Catalog, and Integrated Security elements, which you should recognize from earlier exercises . When you build the application, the C# compiler copies the app .config file to the folder holding the compiled code and renames it as application .exe .config, where application is the name of your application . When your application connects to the database, it should read the connection string value from the configuration file rather than using values that are hard-coded in your C# code . You will see how to do this when using generated entity classes later in this chapter . You should deploy the application configuration file (the application .exe .config file) with the executable code for the application . If you need to connect to a different database, you can edit the configuration file by using a text editor to modify the attribute of the element . When the application runs, it will use the new value automatically . Be aware that you should take steps to protect the application configuration file and prevent a user from making inappropriate changes . 574 Part V Managing Data Now that you have created the entity model for the application, you can build the user inter-face that can display the information retrieved by using data binding . Create the user interface for the Suppliers application . 1 . . InSolution Explorer, right-click the MainWindow .xaml file, click Rename, and rename the file as SupplierInfo .xaml . . 2 . . Double-click the App .xaml file to display it in the Design View window . In the XAML pane, change the StartupUrielement to “SupplierInfo .xaml”, as shown next in bold type: ... . 3 . . In Solution Explorer, double-click the SupplierInfo .xaml file to display it in the Design View window . In the XAML pane, as shown in bold type in the following code snip-pet, change the value of the x:Class element to “Suppliers .SupplierInfo”, set the Title to “Supplier Information”, set the Height to”362”, and set the Width to “614”: ... . 4 . . Display the SupplierInfo .xaml .cs file in the Code and Text Editor window . Change the name of the MainWindow class to SupplierInfo, and change the name of the constructor, as shown next in bold type: public partial class SupplierInfo : Window { public SupplierInfo() { InitializeComponent(); } } . 5 . . In Solution Explorer, double-click the SupplierInfo .xaml file to display it in the Design View window . From the Common WPF Controls section of the Toolbox, add a ComboBox control and a Button control to the form . (Place them anywhere on the form .) From the All WPF Controls section of the Toolbox, add a ListView control to the form . . 6 . . Using the Properties window, set the properties of these controls to the values specified in the following table . ... - tailieumienphi.vn