Learning Friday: Entity Framework 7

ah… scratch that, Entity Framework Core 1.0
Recently, one of my peers and I were tasked with creating a database to help us select and identify the different technical skills (PowerShell vs C# vs ASP, etc) that we expect our team members to have as they progress through the technical career track.  Anyway, the details of the exercise are very uninteresting, but what is interesting is that I was able to use this as a chance to explore Entity Framework 7 Entity Framework Core and as a chance to share what I learned with most of the developers in my organization.
Entity Framework 7 Entity Framework Core recently hit release candidate status and there have been some serious changes under the hood to how this ORM works.  Entity Framework has always been build around the EDMX for creating the model to map between you code and the database.  In Entity Frame work the concept of the “Code First” model was introduced to eliminated the need for using the XML based model files and allow developers to build their models using code only.  This code first approach still relied on all the EDM bits inside Entity Framework to actually work.  In EF7 EFC, that model has been retired and the code first method is the way to build your database models with all the EMD bits being left behind.

At Associated we have expectations that all of our people are leaders, to some degree, and that all team members will be effective communicators in both written and verbal communication skills.  It is an emphasis that I have not seen in other organizations but I have see first had the costs of poor communication insted time in meetings and failure to properly execute on projects.  So, to promote these skills, and to promote a culture of learning, we created the “Tech Faire” platform.  It started as an every other Friday activity where people gave 20-20 style pecha kucha style presentation over a work related (usually technical since this started in our development departments) subject.  I would be an opportunity to work on your communication and presentation skills and teach the rest of the department something.  Well, the Tech Faire has grown company wide and now it is happening on a much less frequent basis.  To continue promoting learning and nudging people to teaching others and growing their communication skills, I ran the first “Learning Friday” activity.  My idea is that these activity are 30 minute classes where you dive deeper into a technical subject that you would in the pecha kucha presentation.  It is my strong belief that our senior employees should be doing something like this to promote and grow our junior employees.  This Learning Friday platform is intended to fill the gaps between our Tech Faires and provide a new (smaller and less intimidating) platform to teach technical skills and grow communication abilities.

I used the exercise that I discussed above as a chance to explore Entity Framework 7.  Building as simple ASP.NET site that talked to a database (with EF7) took about 8 hours from zero to production ready, dependency injection, unit testing, the works.  I then spend about 1 hour stripping out the non-EF7 bits and grabbing some screen shots to create some documentation.  So for a total investment of 1 hour above what I was already asked todo, I had everything ready to share my knowledge about EF7 with our other developers.  My goal was to have 6 people (one from each development team) join me to explore EF7.  The results were amazing.  I had 18 people join in, almost all our developers and database administrators.  During this exercise I walked through created a database with Sql Server Data Tools and how easy it was to ship that to your local database and then ship SQL to our DBAs to create the database on a test/production environment.  Our DBA’s complained about some of the “junk” that the tool generates but they all agree it was a big improvement from “pasting sql into a word document.”  I then demonstrated how to generate a code first model from the existing database, a big concern in the room because EDMX is going away.  I then walked through unit testing with the in-memory database provider and configuration with with SQL provider.  All in all, there was about 1 man day invested in this learning.  For that cost we probably saved several man days of independent research and experimentation and we all gain a common starting point and understanding of EF7 and the code first design approach.  In my opinion, time well spent.  Below is the documentation I created for this exercise if you would like to see or following along your self.

Start with a web project, unit testing project and SSDT database project with your tables modeled.

Using SSDT to compare databases
Follow these steps to push you schema to a local database
Pushing Schema changes with SSDT
Steps for created an Entity Framework model
creating entity framework objects from existing database
Select code first (note I’m still using EF6 Power Tools)
creating entity framework objects from existing database
Here is your code first context.  Note the ModelCreating Method and the simple setup to identify Keys and no null colums.
Entity Framework OnModelCreating
This is our test context, it will use the In-memory provider, to keep the in-memory object available after the controller disposes the context I have overrode the dispose method.
Test implementation for entity framework
Here is our test fixture, not the use of “UseInMemoryDatabase”
Image
Entity Framework 7 is platform agnostic, so there is no concept of Schema or Identity columns, by adding the SQL Server extensions you can get those things into your context.
SQL Server configuration for Entity Framework
Finally, here is how you wire up your context to you connection string.
SQL Server configuration for entity framework