


Public void ConfigureServices(IServiceCollection services) The EF context is configured to use the SQL Server provider. Lastly, the context and MVC is added to the DI container. In case you dont want to use Visual Studio 2017 yet, you can also use EF Core 1.1 with Vsiual Studio 2015 – or use the SingleOrDefault method instead of the Find method. Theres one exception: the Find method of the DbSet type is new with EF Core 1.1. Previously I explained that the sample code can be used with Entity Framework Core 1.0 as well. Public IEnumerable Get() => _booksContext.Books Public BooksController(BooksContext booksContext) Private readonly BooksContext _booksContext Public class BooksController : Controller With the BooksController, the EF context is injected in the constructor, and methods such as Get make use of this context: Public BooksContext(DbContextOptions options) The BooksContext implements the EF Core context and is prepared for dependency injection by using the constructor DbContextOptions: The Book type defines the model that will be used from the EF Core context: The features shown here are also available for Entity Framework Core 1.0. You can easily use Visual Studio 2015 Update 3 with. I’m starting with the empty template and just add a few features for an API controller.įor the sample application I’m using Visual Studio 2017 RC with the.
Unit testing bookz how to#
This article is not about why unit testing is great, it just gives the information how to remove the dependency on a database with Entity Framework Core by using a memory-based provider – a provider that is here for unit tests.įirst, let’s create a ASP.NET Core Web application. This article explains how you can configure Entity Framework Core to use the memory-based provider for unit testing. You can use the same context you use with SQL Server (or other providers) with the memory-based provider. For easier unit testing, Entity Framework Core offers a memory-based povider.
