ProjektX – Starting steps with Entity Framework

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:

  1. Update the Entity model in Visual Studio and then update the database from this.
  2. 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:

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:

DBeaver

Master/Detail Tables

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:

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:

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:

 

Share this:
0 votes

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">