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.
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.
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.
Finally add two FlexGrids to the form, one below the other, and a C1DbNavigator below those and then set the following properties: C1FlexGrid1
DataSource
C1DataSource1
DataMember
Orders
ControlHandler on C1DataSource1: AutoLookup
True
C1FlexGrid2
DataSource
C1DataSource1
DataMember
OrderDetails
ControlHandler on C1DataSource1: AutoLookup
True
C1DbNavigator1
DataSource
C1DataSource1
DataMember
Orders
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!