Aspnetcore.docs: Project fails to run after step "Add the database context to the Startup.cs file:"

Created on 8 Jun 2018  Â·  6Comments  Â·  Source: dotnet/AspNetCore.Docs

I add the following to my startup.cs file and get the following error located below.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();

      services.AddDbContext<MvcMovieContext>(options =>
              options.UseSqlite("Data Source=MvcMovie.db"));

  }

'DbContextOptionsBuilder' does not contain a definition for 'UseSqlite' and no extension method 'UseSqlite' accepting a first argument of type 'DbContextOptionsBuilder' could be found (are you missing a using directive or an assembly reference?)
Along with the terminal error:
Startup.cs(40,27): error CS1061: 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlite' and no extension method 'UseSqlite' accepting a first argument of type 'DbContextOptionsBuilder' could be found (are you missing ausing directive or an assembly reference?)

Looking around online doesn't quite help for this particular issue.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P1

Most helpful comment

I managed to fix this issue by installing Microsoft.EntityFrameworkCore.Sqlite 2.1.1

  1. Install dotnet CLI if you haven't
  2. Get the file path of the MvcMovie.csproject file
  3. Run this command dotnet add [filepath] package Microsoft.EntityFrameworkCore.Sqlite --version 2.1.1

Hope this helps

All 6 comments

I had a similar issue that was resolved by modifying the MvcMovie.csproj file. I replaced

<PackageReference Include="Microsoft.AspNetCore.App" />

with

<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.0" />

The documentation references the Microsoft.AspNetCore.All package but the template used to generate the project uses Microsoft.AspNetCore.App. Some of the differences in the dependent packages that are loaded by All but not by App are packages related to Sqlite.

@zarthur We strongly recommend you not use M.A.All. You're better off using "Microsoft.AspNetCore.App" with the current version.

@jball5 UseSqlite is not in M.A.App, that's why you got that error. You need to explicitly add it. See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/metapackage?view=aspnetcore-2.1#migrating-from-microsoftaspnetcoreall-to-microsoftaspnetcoreapp

Also appears in https://github.com/aspnet/Docs/blob/master/aspnetcore/tutorials/razor-pages-vsc/model.md

See: Razor page Tutorial doesn't tell user to install the SQLite provider #7516

I managed to fix this issue by installing Microsoft.EntityFrameworkCore.Sqlite 2.1.1

  1. Install dotnet CLI if you haven't
  2. Get the file path of the MvcMovie.csproject file
  3. Run this command dotnet add [filepath] package Microsoft.EntityFrameworkCore.Sqlite --version 2.1.1

Hope this helps

image

This worked for me

This was fixed in the 2.2 unification.

Was this page helpful?
0 / 5 - 0 ratings