Tuesday, July 5, 2016

Visual Studio 2015 Update 3 with DotNetCore 1.0.0 SDK install with Entity Framework 7 ( EF7 )

Had some fun today trying to get EF7 to work with dotnet core, but finally got through all the issues I was running into. Let me take you on that journey:

In order to proceed you've got to install the following three items (perhaps just the first two...):
1. Visual Studio 2015 Update 3
2. .NET Core for Visual Studio
3. or choose the installer from https://www.microsoft.com/net/download which I perhaps shouldn't have done.

Links for the above can be found in this article which I also use in my explanations below:
https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html

I installed 1 above, then 2, but was still getting an error. In particular I couldn't get this line to run from this tutorial.
 Install-Package Microsoft.EntityFrameworkCore.Tools –Pre  


To troubleshoot I installed the x86 versions of .NET Core SDK Installer (Preview 2) and .NET Core Installer (v1.0) as well to see if that would help. It did until I rebooted after which no references were being displayed. I quickly reinstalled the .Net Core SDK Installer (Preview 2) x64 versions again and that seemed to do the trick.

All three of the install package commands were now installed without restore failures!:
 Install-Package Microsoft.EntityFrameworkCore.SqlServer  
 Install-Package Microsoft.EntityFrameworkCore.Tools –Pre  
 Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design  


Next I tried to get EF7 to work with a raw .Net Core project following the efproject.net link's instructions which needed a little changing.

First change the project.json to the following:
 {  
  "version": "1.0.0-*",  
  "buildOptions": {  
   "emitEntryPoint": true  
  },  
  "dependencies": {  
   "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",  
   "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",  
   "Microsoft.EntityFrameworkCore.Tools": {  
    "type": "build",  
    "version": "1.0.0-preview2-final"  
   },  
   "Microsoft.NETCore.App": {  
    "type": "platform",  
    "version": "1.0.0"  
   },  
   "Microsoft.EntityFrameworkCore.Design": {  
    "type": "build",  
    "version": "1.0.0-preview2-final"  
   }  
  },  
  "tools": {  
   "Microsoft.EntityFrameworkCore.Tools": {  
    "version": "1.0.0-preview2-final"  
   },  
   "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",  
   "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"  
  },  
  "frameworks": {  
   "netcoreapp1.0": {}  
  }  
 }   


Key point 1: Can't use EF7 against a class library so we added the buildOptions and emitEntryPoint portion per this article. EF7 won't work against a project that is not an application of some sort (i.e. it can't be a class library). Otherwise you'll see these kinds of errors when you try and run Scaffold-DbContext:
 PM> Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer  
 Could not invoke this command on the startup project 'Core.Datav2'. This preview of Entity Framework tools does not support commands on class library projects in ASP.NET Core and .NET Core applications. See http://go.microsoft.com/fwlink/?LinkId=798221 for details and workarounds.  

or if you forgot to add the [STAThread]...static void main(string[] args) in you're Program.cs:
 PM> Scaffold-DbContext "Server=localhost;Database=VbscriptIndexer;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer  
 C:\Program Files\dotnet\dotnet.exe compile-csc @C:\Projects\dotNetCore\Core.Datav2\bin\Core.Datav2\obj\Debug\netcoreapp1.0\dotnet-compile.rsp returned Exit Code 1 C:\Projects\dotNetCore\Core.Datav2\error CS5001: Program does not contain a static 'Main' method suitable for an entry point   
  Build failed on 'Core.Datav2'.   
And if you're still having issues, re-install the .NET Core SDK Install (Preview 2) and try again. I ended up doing it a few times which was a bit annoying but was really self-caused misery.

No comments:

Post a Comment