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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
{ "version": "1.0.0-*", "compilationOptions": { "emitEntryPoint": true }, "dependencies": { "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", "EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final", "EntityFramework.Commands": "7.0.0-rc1-final", ... other project dependencies }, "commands": { "web": "Microsoft.AspNet.Server.Kestrel", "ef": "EntityFramework.Commands" }, ... } |
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
1 |
"ef": "EntityFramework.Commands" |
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
1 2 3 4 5 6 7 8 9 |
{ "Logging": { ... }, "Data": { "ConnectionString": "..." } } |
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.
1 2 3 4 5 6 7 8 |
public Startup(IHostingEnvironment env) { // Set up configuration sources. var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json"); builder.AddEnvironmentVariables(); Configuration = builder.Build().ReloadOnChanged("appsettings.json"); } |
After that you also have to configure Entity Framework in Startup.cs.
1 2 3 4 5 6 7 8 9 10 11 12 |
public void ConfigureServices(IServiceCollection services) { ... services.AddEntityFramework() .AddSqlServer() .AddDbContext<AppContext>(options => { options.UseSqlServer(Configuration["Data:ConnectionString"]); }); services.AddScoped<AppContext, AppContext>(); ... } |
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.
Pingback: ASP .NET 5 Identity with Entity Framework 7 Setup - illucIT Blog