Using DI, Logging, and Configuration in a .NET Core Console App

Yesterday set about building a small, Core Console Application that does some data access. A very early challenge for me was how to store my connection string. I’m used to ASP.NET Core work, where the configuration is set up in the Startup class, but a plain old console app starts out none of that sweet infrastructure.

I set about frenzied Googling for anything, and it was a good day for me because I found Chad Ramos‘ article titled:

Dependency Injection, Logging and Configuration In A .NET Core Console Application

It is a very simple, three-step tutorial, very readable and with a single, code example used across all three steps. Chad includes a link to a very readable and neat example if you want more than his code excerpts in the article. It may be easier to just use his code as a startup template for such projects.

It has one small problem in that you can’t e.g. reference a class and call a method from another project because the startup code won’t have run, but this is very rare with console apps.

I am so impressed I’m going to make a project template for Core console apps ready to go, with DI, Configuration, and Logging.


DLL Hell returns: EF Core (EF 7) scaffolding is broken by version mismatches.

I have spent the last several hours trying to use EF Core’s CLI scaffolding to reverse engineer (or scaffold) a small database into entity classes. First, it simply does not support scaffolding into a Core (or any) class library. Scaffolding is done with a command much like the following one.

dotnet ef dbcontext scaffold "data source=(local);initial catalog=HawkHead;integrated security=SSPI;multipleActiveResultSets=True" "Microsoft.EntityFrameworkCore.SqlServer" -c HhDbContext -o Data

Executing this on a class library results in an error that simply tells you that scaffolding to class libraries is not supported. One would think a quick and easy workaround would be to create a .NET Core Console Application, but then executing the above command results in any number of intractable exceptions. The most understandable one was that “Microsoft.EntityFrameworkCore.SqlServer.Design”: “1.0.1” was not compatible with “Microsoft.EntityFrameworkCore.Tools”: “1.0.0-preview2-final”. There is no higher version for “Tools”, despite MS bragging that Core was now RTM and will save the fucking world.

There is, however, a very simple workaround. Add an old fashioned .NET 4.6 Console Application to your solution, add EF 6 to it, use EF 6 to do your scaffolding, and because the entity classes are POCOs, you can just copy and paste them all into your new, trendy .NET Core class library, and they work just the same with EF Core (EF 7).

Erisia Software has a New Home.

This site will feature case studies of Erisia Software in delivering top quality information solutions, as well as news on information technology in general, and some educational material on software development. For now, this blog will concentrate of Angular 2 and .NET Core.

But right now, my clients are much more important than my website, so content will slowly begin to appear over the next week or two, and then later more regularly.