Wednesday, November 18, 2015

Getting Started with EF7 - Adding EF7 to a new Project

While on the Gold Coast at Ignite, I presented on the new version of Entity Framework (EF7) while my colleague Jon spoke about the broader topic of .NET vNext.

In my talk, I gave 4 demos:
  1. Walkthrough showing how to add EF7 to a new project
  2. Using SQL Profiler to show the queries that EF7 generates for various scenarios
  3. Adding Migrations and Seeding to your application
  4. Using the new InMemoryProvider to easily unit test code that depends on EF7 data contexts
For the first demo I really wanted to show how simple it is to get started with EF7 and how the new component architecture works with respect to Nuget packaging.

For my first demo, I started off by creating a new Console Application (Package) project from the Web Templates.

The Solution must be configured so that the runtime version is aligned with a runtime that you have installed and Nuget is knows which Package Source contains the versions of the dependencies that you want to use.

To find which runtime versions you are installed on your machine, use the DNVM list command:

This shows that my machine is currently configured to use the 1.0.0-rc2-16183 clr x64 runtime, so the first thing I do is to change the global.json solution file to match.

  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-rc2-16183"

Nuget needs to know where to look when it restores packages.  This can be done by adding it in the Nuget.Config file.

In this case the ASPNETCIDev source which is hosted on MyGet is where I want to get the EF packages from as contains the most recent packages from the ASPNET daily CI build process.

<?xml version="1.0" encoding="utf-8"?>
    <clear />
    <add key="" value="" />
    <add key="aspnetcidev" value="" />

The last thing to do is to add references for the EF7 dependencies that I need. I this case I'm after the following 3 packages:

  1. EntityFramework.Core: Contains core logic for DbSets, DbContexts, DataAnnotations, Querying, ChangeTracking, and Configuration among other things. 
  2. EntityFramework.Commands: A set of commands that can be used to create Migrations, Update Databases, and to scaffold an application from an existing database.
  3. EntityFramework.MicrosoftSqlServer: A database provider for using EF against a Microsoft SQL Server database.

Each of these packages represent a single project in the EntityFramework repository which is available to view in this GitHub repository.

"dependencies": {
  "EntityFramework.Core": "7.0.0-*",
  "EntityFramework.Commands": "7.0.0-*",
  "EntityFramework.MicrosoftSqlServer": "7.0.0-*"
"commands": {
  "ConsoleApp10": "ConsoleApp10",
  "ef": "EntityFramework.Commands"

The "ef" command which is added to the project commands can be used to run the EF commands using DNX.

At this point, EF7 is configured and available to use. To test this, jump to the command line at the root of your project and type dnx ef. You should see the Magic Unicorn splash screen.

I finished the demo by creating a simple DbContext which contained a  couple of DbSet entities and created a Migration  using the EntityFramework Commands so that I could start working against a database.

What I hoped to achieve through this demo was to show the configuration points and how they connect the application to its environment and dependencies.

It is important to take note of the benefits that are achieved from the EF7 'ground up' rewrite which has delivered multiple, lightweight packages which empowers application developers to take only what they need in terms of dependencies.  Don't need Commands?  Simple, don't take that dependency!  Over time, this will enable more rapid innovation from Microsoft's end and increase flexibility and performance on the applications side.

If you are interested in taking a look at the sample code from my demos, you can find it in this GitHub repository.

No comments:

Post a Comment