Sunday, December 18, 2016

Creating a .NET Core NuGet package with "dotnet nuget" SDK command

I've lately been working some with .NET Core as a host for a new Web API, and have been working with EF Core to handle data access logic.

One thing that I found useful was to create a generic repository abstraction for some core EF behavior, instead of needing to create a repository class for every model class.  I found it was possible to create some generic behavior for Add, Update, Delete, and FindById, and leave anything beyond that to custom search methods.

Instead of having this behavior embedded into the individual projects, I decided to pull the behavior into a NuGet package, which I've called EntityFrameworkCore.GenericRepository, and is available on NuGet now.

For those of your who may not have worked with .NET core class libraries, the existing "dotnet pack" command line method will actually create a NuGet package for a class library automatically.  But it doesn't provide a way to set a lot of the properties that should normally be configured on a package. 

So, to get some of these additional properties, you need to use either NuGet.exe, or you can use some new functionality that's only available in the preview3 (or later) .NET Core SDK.   If you're feeling adventurous, you can download the latest bits and install from a link here:

https://github.com/dotnet/cli

There's a link down the page to SDK installer, which maps here:

https://dotnetcli.blob.core.windows.net/dotnet/Sdk/rel-1.0.0/dotnet-dev-win-x64.latest.exe

After installing this version of SDK, you now can execute following command line:

dotnet nuget pack .nuspec

Run this in the directory of your project where the .nuspec file is located (or pass a path to .nuspec) and this will generate the package for you, and properly use the .nuspec file values.

If you're interested in trying out this generic entity framework behavior, you can find the NuGet package info here:

http://www.nuget.org/packages/EntityFrameworkCore.GenericRepository/

The source code is available, with the .nuspec file I used, is here:

https://github.com/kmkatsma/EntityFrameworkCore.GenericRepository

This currently is using 1.1 version of core, targeting netstandard1.6 (It was necessary to go to 1.1 to support FindById).  If you're using EF Core with 1.1, try it out, and let me know what you think!