AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

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

Open jball5 opened this issue 8 years ago • 6 comments

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.

jball5 avatar Jun 08 '18 05:06 jball5

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 avatar Jun 08 '18 18:06 zarthur

@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

Rick-Anderson avatar Jun 09 '18 01:06 Rick-Anderson

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

guardrex avatar Jul 08 '18 06:07 guardrex

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

jasmine-nguyen avatar Aug 12 '18 03:08 jasmine-nguyen

image

This worked for me

JesseAReyes1996 avatar Sep 08 '18 02:09 JesseAReyes1996

This was fixed in the 2.2 unification.

Rick-Anderson avatar Jan 14 '19 23:01 Rick-Anderson