If you're planning to get started with Entity Framework 7, at first you need to configuring the entity framework. The default is already there but when you need to update to the new versions, you must take a look at what is in the project.json file:

{
  /* Click to learn more about project.json  http://go.microsoft.com/fwlink/?LinkID=517074 */
  "webroot": "wwwroot",
  "version": "1.0.0-*",
  "dependencies": {
    "EntityFramework.SqlServer": "7.0.0-beta3",
    "EntityFramework.Commands": "7.0.0-beta3",
    "Microsoft.AspNet.Mvc": "6.0.0-beta3",

For your information, the startup.cs allowed for configuration to happen in two phases. First, the configuration of the different elements, then the registering of services with the dependency injection layer.

The service container is the dependency injection mechanism built into ASP.NET 5 and the ConfigureServices method is where that happens. Before you can add EF to the DI container, you need to get a connection string.

When I first came to ASP.NET 5, it looked as if the EF configuration would just load the connection string by convention, but it didn’t work. So I had to get it manually and add it (below) when I configured the context object.

// This method gets called by the runtime.
public void ConfigureServices(IServiceCollection services)
{
  var connectionString = Configuration.Get("Data:DefaultConnection:ConnectionString");

The Configuration.Get method allows it to read a setting from the list of configuration sources. Then, the Configuration object is a merge of the config.json file and any environment variables. In the case of the connection string, you’re looking for a string called “ConnectionString” inside an object graph. This should look obvious once you see the config.json file:

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;..."
    }
  },
  "EntityFramework": {
    "ApplicationDbContext": {
      "ConnectionStringKey": "Data:DefaultConnection:ConnectionString"
    }
  }
}

Now that we have the connection string, what's next?

You can add Entity Framework to the services collection by adding it this code below:

// Add EF services to the services container.
services.AddEntityFramework(Configuration)
    .AddSqlServer()
    .AddDbContext<MyCountriesContext>(options =>
    {
      options.UseSqlServer(connectionString);
    });

SUMMARY

The AddEntityFramework method adds EF to the dependency injection container so it can be served if needed later. Additionally, calling AddSqlServer specifies the data store you’ll be using. Finally, the AddDbContext adds a DbContext object to the EF service. The options lambda allows us to specify the connection string. I suspect this extra step will go away at some point and just read from the configuration by convention, but at this point it’s necessary. If we need the context in another part of the system (e.g. Controllers) we can just let ASP.NET 5 serve it to us in the constructor like any other dependency injection framework.

HostForLIFE.eu ASP.NET 5 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.