Tel Map

Blog

ASP .NET 5 with Entity Framework 7

Adding Entity Framework 7 Packages to ASP .NET Project

Older versions of the Entity Framework need a full .NET runtime in order to work. If at some point you want to use the new .NET 5 Core e.g. to run your application on Linux etc., you should use Entity Framework 7.

project.json:

A few comments regarding my project dependency setup. Of course, first of all you have to add the Entity Framework libraries as dependencies.

Make also sure to add

under the commands section. This adds additional functionality to the dnx command for Entity Framework. To see how to set up the new command tools for ASP .NET 5 see How to set up dnx . After that you can use dnx ef to for example do database migrations or scaffold a database context from an existing database. Make sure to setup the application properties including the ConnectionString exactly as described below. Otherwise the ConnectionString from the application properties can not be found automatically and you have to manually provide the ConnectionString for each dnx ef command.

Database ConnectionString

Previous to ASP .NET 5 the ConnectionString has been stored in the App.config or Web.config. This file does not exist in ASP .NET 5 projects, however.

appsettings.json

Storing the ConnectionString exactly like this, will make sure it also can be found dnx ef.

In the file Startup.cs make sure that this application configuration file is loaded properly.

After that you also have to configure Entity Framework in Startup.cs.

This configures Entity Framework 7 to use the ConnectionString provided in the appsettings.json configuration file.

Also note that I used services.AddScoped to later on provide my database Context AppContext to Controllers via the new ASP .NET 5 Dependency Injection. This way if you simply add AppContext as a parameter to the constructor of a controller, the new DI will automatically provide it.

Summary

This article demonstrates how to setup an ASP .NET 5 project with Entity Framework 7. In the next article I will describe how a database context can be scaffolded from an existing database and how ASP .NET 5 Identity framework can be used to provide authentication in combination with Entity Framework 7.

 

One Reply to “ASP .NET 5 with Entity Framework 7”

  1. Pingback: ASP .NET 5 Identity with Entity Framework 7 Setup - illucIT Blog

Leave a Reply

Your email address will not be published. Required fields are marked *