This post follows on from my previous comments on the Orcas Beta 1
The first thing you will need to do if you want to work with the Entity designer in Orcas Beta 1 is to download and install the fix (see my previous post). This does NOT appear to include the drag-n-drog design surface that will enable you to redesign your entities but does at least provide a UI interface to the EDM tool.
The first step in building an application that uses Entities to pull data too and from the database is to add a Data Model to your project. You can do this via the usual Add New Item dialog:
Add New Item - ADO.NET Entity Data Model
Select Generate from Database, Unless you want to code everything by hand!
Using the familiar New Connection button you can set up a connection to your database
Next, choose the database object you want to import as entities.
You will notice it adds three model files and a generated code file
Although the individual class files don't exist, you now have a series of classes defined that map to the database objects you previously selected. If you open up the Class View window you will see them.
The next thing to do is to setup the databinding and the UI. In the same way as you could add an object binding source in [VS2005] you can do the same thing via the Add Data Source item on the Data Menu. Selecting Object as the data source will present you will a list of objects to choose from.
Notice the entity classes are all in a generated namespace. In our case we have selected the Manufacturers class. The Entity generator still doesn't cater well for moving between singular and plural names!
Once we have added an Object Data Source we can use the Data Sources window to drag-n-drop either a grid or details onto our form.
Select the Data Grid View
Tidy up the form and remove unwanted columns - To remove these out of the entity model is a bit of a mission as you have to remove them from the 3 model files.
Next we need to add a little bit of code to the Load event of the form so that we can populate the data grid:
Public Class ManufacturersForm
Private dc As New MobileEntities (1)
Private Sub ManufacturersForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim mans = dc.Manufacturers.ToArray (4)
Dim manufacturers = From manufacturer In mans _ (2)
Where manufacturer.MAN_Name.Length > 5 _
Select manufacturer.MAN_Name, manufacturer.MAN_URL
Me.ManufacturersBindingSource.DataSource = manufacturers (3)
- In order to work with our entities we need a data context (think of this as a data manager that is responsible for loading, refreshing and saving your data).
- Lets use a bit of Linq to select Manufacturers with Names with a length greater than 5. Note that we are projecting only the Name and Url fields from the Manufacturer class
- Now apply the data to the binding source. Note that although we built the binding using the full Manufacturer class because we only use the Name and Url fields in the grid we can get away with binding to the anonymous type projected in the previous linq statement
- Unfortunately Linq to Entities is still relatively broken and on occasions you will have to force a pre-evaluation of the query in order for the following query to execute!
We now have a working sample - How easy was that!
This shows you the very basics of how to bind (single-direction) entity data to a ui grid.