locked
Contoso University - DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer' RRS feed

  • Question

  • User281366353 posted

    I am trying to work through the Contoso University example application.  I have simply copied the Startup.cs class statements:

    services.AddDbContext<SchoolContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    I receive the error:

    DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer' and no accessible extension method. 'UseSqlServer' accepting a first argument of type 'DbContextOptionBuilder' could be found ( are you missing a using directive or an assemply reference)

    What do I need to do to move forward?

    Thanks

    Thursday, May 14, 2020 4:30 PM

Answers

  • User281366353 posted

    Thank you for your response.  I did delete the project and start over.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 16, 2020 1:48 PM

All replies

  • User475983607 posted

    What do I need to do to move forward?

    You missed a the scaffolding step.  This step creates adds the required packages, connection string, and updates the startup.cs file. 

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-3.1&tabs=visual-studio#scaffold-student-pages

    Thursday, May 14, 2020 6:53 PM
  • User711641945 posted

    Hi Geol4549,

    Add the reference like below:

    using Microsoft.EntityFrameworkCore;

    Best Regards,

    Rena

    Friday, May 15, 2020 1:44 AM
  • User-1955300613 posted

    Hi Geol4549,

    Thanks for your question,

    The following maybe a solution:

    If you use the EF Core, you must add  ' Microsoft.EntityFrameworkCore.SqlServer ' to your reference,

    and then don't forget to apply   ' using Microsoft.EntityFrameworkCore ' to source file.

    Best regards,

    Tsaisoul.

    Friday, May 15, 2020 2:47 AM
  • User281366353 posted

    I'd like to thank those who replied, but I can't  run the scaffolding procedure because in the Program.cs file error: 

    CreateWebHostBuilder does not exist in the current context. 

    Friday, May 15, 2020 8:53 PM
  • User475983607 posted

    Geol4549

    I'd like to thank those who replied, but I can't  run the scaffolding procedure because in the Program.cs file error: 

    CreateWebHostBuilder does not exist in the current context. 

    It's not real clear how you got to this point.   Delete the project and start over or download the completed project and compare the difference to your code.

    Saturday, May 16, 2020 6:44 AM
  • User281366353 posted

    Thank you for your response.  I did delete the project and start over.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 16, 2020 1:48 PM