DbContextOptionsBuilder could not be found ASP.NET Core 1.1 Web API project

.net-core asp.net-core azure c# entity-framework-core

Question

I'm trying to add Entity Framework Core 1.1 to my project and use a connection String to get my data from a Azure database. I tried reading the changes on https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-entity-framework-core-1-1/ where they say, that i should do this in my Startup.cs file to connect to the DB:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseSqlServer(
            "connection string",
            options => options.EnableRetryOnFailure());
}

But it just gives me an error:

The type or namespace 'DbContextOptionsBuilder' could not be found...

and

'Startup.OnConfiguring(DbContextOptionsBuilder): No suitable method found to override.

I can add using Microsoft.EntityFrameworkCore; which will make the error with DbContextOptionsBuilder disappear, but then the .UseSqlServer is getting an error:

'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer' and no extension method ...

This is my dotnet.csproj file dotnet.csproj

How to fix this problem, so i can connect to my Db on Azure and get the data?

Thanks in advance!

1
1
3/22/2017 11:01:58 PM

Accepted Answer

The blog post you're referring to might be a bit confusing. They're talking about the OnConfiguring method of your DbContext or some place in your Startup. Your example is mixing both worlds (you expect the DbContext OnConfiguring method to be in the Startup).

First add the package Microsoft.EntityFrameworkCore.SqlServer.

Let's say you want to use Startup.cs, it will look like this:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<YourDbContext>(options =>
        {
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
        });
    }

You can define your connection string in the appsettings.json:

{
    "ConnectionStrings": {
        "DefaultConnection": "Data Source=.;Database=<your db>;User Id=;Password="
    }
}
1
3/23/2017 9:13:09 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow