1

I'm currently developing an ASP.NET 5 Web-API application with VS2015 Ultimate Preview. Some things have changed about configuring EF7 on this new platform.

I've already checked the help in this page: https://github.com/aspnet/EntityFramework/wiki but it doesn't show all the step needed to successfully complete a connection with EF7 (it shows only a partial answer)

Can anyone bring a step-by-step tutorial on how would be the correct way to connect to a database (SQL Server) using EF7?. (not using old syntax like in MusicStore sample app but using more recent syntax)

3
  • Creating a new ASP.NET 5 Starter Web project shows a good starting point. Look at Startup.cs and config.json. Commented Dec 29, 2014 at 17:30
  • Define more recent syntax please. The MusicStore app has a master branch which is for beta3 development currently and a dev branch which should be using the latest (beta4ish) code. Commented Mar 20, 2015 at 13:57
  • basically what he is saying is that the musicStore app needs to be turned into a wiki so that people like netcito and myself can understand wtf is going on. Im currently wrestling with dnvm and dnx utility. Drips and drabs about that stuff too Commented Jul 3, 2015 at 0:23

3 Answers 3

3

The code should be the same as you linked in the sample app. You register the context in Startup.cs, within ConfigureServices method using the following code:

public void ConfigureServices(IServiceCollection services)
{
    // Add EF services to the services container.
    services
        .AddEntityFramework(Configuration)
        .AddSqlServer()
        .AddDbContext<MyDbContext>(options =>
         {
             options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"));
         });
}

Then your MyDbContext will be available for dependency injection, and in your controllers you can do

public MyController(MyDbContext context)
{
   ...
}

That's it

Sign up to request clarification or add additional context in comments.

1 Comment

that seems like there is a lot of "magic" behind those extension methods. Any documentation regarding what do they do exactly?
2

The following tutorials helped me :

  1. Introduction to Web API : Part 2 – Integrating with Entity Framework 5 Code First

  2. Generic Repository Pattern with Entity Framework and Web API

  3. Building an ASP.NET MVC4 Application with EF and WebAPI

  4. Entity Framework Code First and ASP.NET Web API

  5. Getting started with ASP.NET 5 MVC 6 Web API & Entity Framework 7

Comments

0

Stephen Walther has an updated Music Store tutorial. It starts here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.