In this post we will walk through using the C1DataSource for Entity Framework in a new WinForms project. For those of you who are already familiar with C1DataSource, and creating entity models in general, much of this post will be familiar, but there are a couple of points which may be of interest. We will create a very simple, one-form application that will allow you to scroll through all of the orders created by the Northwind trading company and display the detail lines of those orders. We’ll achieve this without writing a single line of code! Begin by creating a new project in Visual Studio and save it. Saving the project at this stage, even though we have yet to do anything, avoids an annoying little warning message that appears when you create an entity data model in an unsaved project. Now follow the steps illustrated below to create your new Entity Data Model. Begin by right clicking on the project in solution explorer and selecting Add - New Item. In the dialog that opens, select "ADO.Net Entity Data Model." EF6.x requires .Net 4.5 or higher. The Entity Data Model Wizard will now start. In the first dialog select "Generate from Database." Then set the connection string to the database (and elect to have it stored in the project’s App.Config file). Now you’ll be asked to select the version of the Entity Framework against which you wish to build the model. Choose Entity Framework 6.0. Then choose the objects in the database that you wish to model (we’ll just select all tables for this example). The model will then be created and its diagram will open up in Visual Studio. Save and build your project. There are now a few steps we need to take to make this model compatible with C1DataSource.
- In solution explorer click on the icon in the toolbar to show all files in the current project.
- Then expand the node next to model1.edmx.
- Delete the Model1.Context.tt file.
- Delete the Model1.tt file.
- Right click on the surface of the model diagram and from the context menu that pops up select "Add Code Generation Item…"
Once the Add Code Generation Item Dialog opens select the code generation item you want to use. In this case we’ll select the item marked ‘1’ in the illustration below; "ComponentOne EF 6.x DbContext Generator."
Note: If you wanted to use the ObjectContext instead of the DbContext we could have selected item 2 in the illustration below; "EF 6.x EntityObject Generator." This item is not part of the standard installation of Visual Studio, it is in fact an extension which can be freely downloaded and installed from here: http://visualstudiogallery.msdn.microsoft.com/site/search?query=EF%20EntityObject&f%5B0%5D.Value=EF%20EntityObject&f%5B0%5D.Type=SearchText&ac=5 Ensure that you select the one appropriate to your requirements.
Now simply rebuild your project and you will have an Entity Data Model suitable for use with C1DataSource. To finish the application return to the design surface for the default Form1 of your application. Drag a C1DataSource from the toolbox and add it to the form (it will sit in the tray beneath the form itself). Right click on the C1DataSource component and select Properties from the context menu.
- In the dropdown next to ContextType select the name of the Entity model you had previously created (such as NorhtwinEntities).
- Open up the ViewSourceCollection editor by clicking the ellipsis next to ViewSourceCollection.
For those of you who have used the C1DataSource before, note the change of property name from ObjectContextType to just ContextType. This reinforces the fact that the C1DataSource can now be used with both the Original ObjectContext and also with the new DbContext. It is up to you to decide which model type you wish to use.
- Select Orders from the drop down that appears next to EntitySetName and then click OK.
Finally add two FlexGrids to the form, one below the other, and a C1DbNavigator below those and then set the following properties: C1FlexGrid1
ControlHandler on C1DataSource1: AutoLookup
ControlHandler on C1DataSource1: AutoLookup
Save, build and run the application. Note how as you select different orders via the C1DbNavigator that the order details for that order are automatically shown in the lower grid. Note too how the grids’ show Customer and Product names as they should be, the information being drawn from other related tables because we set the AutoLookup property to True on the ControlHandler. We now have a fully working, master-detail application without having to write a single line of code!