DotNet Core and EF: Database First

In a LocalDB (SQLServer), I created 2 tables, City and Customer, with FKs from Customer to City. I populated them, and you can get the backup of those here.

From there, I wanted to scaffold out the POCOs, something like this:

That was achieved by this set of PowerShell commands, and some massaging of the content of the project.json (mostly to add SQLServer Tools to the default Web option, which by default right now references SqlLite):

cd C:\sandbox\dotnet
dotnet new -t Web
dotnet restore
dotnet ef
where.exe dotnet
dotnet --info
dotnet ef dbcontext scaffold "Server=(localdb)\MSSQLLocalDB;Database=MyDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o \temp

adsf

poco04

DotNetCore and EF: “The specified framework… was not found.”

In late December 2016 I find that the default DotNetCore and default DotNetCore SDK are not playing nicely when it comes to the command line

dotnet ef

I believe I have now found a fail-safe way of getting the command to work, without playing around with project.json. That solution simply means selecting the LTS (Long Term Support) version of the DotNetCore runtime rather than the Current version. But it took me a long time to get to that realization.


First, the screenshots showing the 2 errors which blocked any progress on this for some time, and which seem to crop up pretty frequently on a Google:


efworking22

I then went back to a clean-ish machine, defined, as:

  1. no DotNetCore references in Programs and Features – that is both for the runtime and SDK
  2. recursive deletion of c:\Program Files\Dotnet
  3. prove that [dotnet] executing under PowerShell returns [no such command] of somesuch.

These are the key locations for getting the correct combination of files, right now:

So for the runtime, I grabbed the Current / Windows / .NET Core 1.1 runtime – Installer, noting and installing that version of the installer. The fact it is the Current version will be significant later in the post:

efworking06

efworking07

efworking08

Now create a new working folder, e.g. c:\sandbox\t1 and navigate there (admin mode). Prove that [dotnet] now works:

efworking21

All good. Now what happens if we try to run dotnet commands?

efworking09

Fine. Return to the sdk location referenced above:

efworking12

Note that confusingly it is the .Net Core element that is 1.1.0, with the SDK element 1.0.0. Regardless. Now try the [dotnet ef] command again…

efworking22

No executable found matching command "dotnet-ef"

Hm. It requires you to invoke in fact the template Web to get this to work. So do e.g..

dotnet new -t Web

Now try [dotnet ef], and you get a different error:

efworking13

The specified framework 'Microsoft.NETCore.App', version '1.0.0' was not found.
 - Check application dependencies and target a framework version installed at:
 C:\Program Files\dotnet\shared\Microsoft.NETCore.App
 - The following versions are installed:
 1.1.0
 - Alternatively, install the framework version '1.0.0'.

Let’s look at what P&F says we have:

efworking24

1.1.0 both for the Runtime and for the SDK. Yet the message says I have “specified” 1.0.0. No I haven’t. I spent some time trying to marry up the project.json and the project.lock.json content to reconcile what was generated. I couldn’t, suggesting to me that the 1.0.0 requirement is somehow baked into DotNetCore EF in the current version. So then I went back to the downloads area above, and rather than specifying the Current version, I asked for the LTS or Longterm Support Version of the Runtime:

efworking25

efworking26

efworking27

efworking28

So although it says above it wants 1.0.0, maybe 1.0.3 is good enough. We run [dotnet ef] again, and this time we have lift-off:

efworking29

And the installation has created a 1.0.3 runtime location in addition to the 1.1.0 runtime that was already there from the earlier default installation from the download area.

efworking31

That’s all for this article. Another day we will look at scaffolding the EF project.


I will leave in some other screenshots: