ProjektX – Starting steps with Entity Framework
Getting through the startup headaches and EF6 looks like a really powerful tool….
Which database method?
After much searching about and testing, I’ve finally settled on SQLite as the db tech or choice. This can sit as a single db file in the user’s application directory and doesn’t require any client-side db server software installed. I wrote a guide to setting this up HERE.
Code, Database or Model first and subsequent editing
Entity Framework lets you pursue 3 approaches to setting up your data model. With my choice, it was between database first and model first. I eventually plumped for the former.
I will need to be adding tables/fields as I go along realistically as I develop the app. Thus I needed to figure how to achieve this. There are two options:
- Update the Entity model in Visual Studio and then update the database from this.
- Update the Database first and then the Entity model from this.
Whilst one would have been preferable due to the VS editor being well designed, it transpired to be quite difficult. There is no actual option in the entity designer to “update db from model” so it has to be done in code, and I couldn’t find a way to get this working. I found some code which was meant to enable this, but sadly no dice. Inserting in the class initialisation:
Database.SetInitializer(New DropCreateDatabaseIfModelChanges(Of mainEntities)())
So it came down to option 2 – edit the database and then update the entity model. Because of this, I was wanting a good SQLite editor with ER (Entity Relationship) Diagramming. This led to a day’s searching only to discover that there are is no free software where you can edit db’s via the ER. However, I did find some software that will automatically generate ER diagrams following you manually constructing tables + keys. This software:
So onto getting things up and running in winforms. You’ll notice above the test tables – so I wanted to display a Master/Detail setup in winforms, with System being the Master record with 2 details forms – controllers and Magazines. Naturally, it wasn’t straight forward at the start. You start by adding a datasource to your project – in this instance the Systm table. Within this, the Magaizines and Controllers tables were automatically imported, leaving the structure like this:
You then drag your master table onto your form. However, in order to get your detail tables syncing up with your master table, you need to do a couple of things:
Add a class to your project:
Public Class ObservableListSource(Of T As Class)
Inherits ObservableCollection(Of T)
Private _bindingList As IBindingList
Private ReadOnly Property ContainsListCollection() As Boolean Implements IListSource.ContainsListCollection
Private Function GetList() As IList Implements IListSource.GetList
Return If(_bindingList, (InlineAssignHelper(_bindingList, Me.ToBindingList())))
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
You then need to alter the entity class of your master table:
Finally, you need to edit your [Model].tt file
- Find and replace the two occurrences of “ICollection” with “ObservableListSource” (lines 296 and 484).
- Find and replace the first occurrence of “HashSet” with “ObservableListSource” ( Line 50).
- Do not replace the second occurrence of HashSet found later in the code.
Then you can drag your detail tables onto the form and they will all sync up. The code is remarkably small and efficient:
Public Class Form1
Dim context As New mainEntities
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SystmBindingSource.DataSource = context.Systms.Local.ToBindingList
Private Sub SystmBindingNavigatorSaveItem_Click_1(sender As Object, e As EventArgs) Handles SystmBindingNavigatorSaveItem.Click
You’ll note that you do have to set the data-source for the binding. However, I’m struck by how easy it is! A quick video of it in action: