I’ve been working with an ASP.NET 5 app that uses an EF6 module. The EF6 module is .Net 4.6.
When re-creating this from scratch, I keep making the same mistake which confounded me for hours the first time and only 15 minutes the second time. I like to think that the next time, I will remember how to do this in advance but decided I better write it down.
Here is the default project.json for a new ASP.NET 5 MVC app that does not have any identity infrastructure set. This was scaffolded directly by the ASP.NET 5 Web App template where I chose to have no authentication included. Also of note is that I’m using RC1.
{ "version": "1.0.0-*", "compilationOptions": { "emitEntryPoint": true }, "dependencies": { "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final", "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final", "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final", "Microsoft.Extensions.Configuration.FileProviderExtensions" : "1.0.0-rc1-final", "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final", "Microsoft.Extensions.Logging": "1.0.0-rc1-final", "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final", "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final" }, "commands": { "web": "Microsoft.AspNet.Server.Kestrel" }, "frameworks": { "dnx451": { }, "dnxcore50": { } }, "exclude": [ "wwwroot", "node_modules" ], "publishExclude": [ "**.user", "**.vspscc" ], "scripts": { "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ] } }
I need to add a reference to my .NET4.6 class library project called EF6Model. This is not an ASPNET5 project. No dnx. No dnu. Just straight .NET. My first instinct is to add it into the dependencies section after ““Microsoft.VisualStudio.Web.BrowserLink.Loader”. It does nothing but fail fail fail. Squiggly lines. Can’t build. Only help is “sort”.
The core of the problem is that the default scaffold specified that this would be for dnx451 and dnxcore50 frameworks. My assembly is not recognized by dnxcore50. In the dependencies section, I am saying that everything in here should work with both frameworks. That’s not true.
If I know I am never going to need the dnxcore50, I can just remove it from the frameworks section:
"frameworks": { "dnx451": { }
},
Also, the default .Net version here is 4.51. My dll is 4.6 so I have to fix that, too.
"frameworks": { "dnx46": { } },
This change will required that you run dnu restore — well at least I had to do that!
You can also specify the dependency directly in the framework section instead of the dependencies section ala:
"frameworks": { "dnx46": { "dependencies": { "EF6Model": "1.0.0-*" } } },
I like this because it explicitly tells me that EF6Model is special and is only supported by dnx46. As I evolve my app and possibly make changes to the dependencies or even the frameworks, I know that what’s in the dependencies section should be supported by any targeted framework, while the framework specific dependencies are very obvious.
Having written this down, I hope now that it is now in my brain and I won’t have to waste time wondering about those errors any more.
Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!
In this setup how do you pass in the connectionstring for the context? In 4.6 you could reference the connectionstring name in the constructor from the .config file but that’s not available when integrating with a 5.0 client app.