C# MVC Create new site fails when adding Controller

Bryan Valencia 1 Reputation point
2021-03-22T07:20:47.663+00:00

VS 2019 Pro
Updated today.
Win 10

Created a new C# MVC site.
.NET Framework 4.8
Added User Authentication using a SQL server database.
Site creates, compiles no errors

Went to Models -> Add New Item -> ADO.NET Entity Model -> code first from database.

Compiled, no problem.

Went to Controllers.
Add new controller -> MVC5 Controller with views, using Entity Framework

Hit GO, get Error Message:

---------------------------
Microsoft Visual Studio
---------------------------
Error

There was an error running the selected code generator:

'Entity Framework 5 or later is required for the current operation but not available. Make sure the project has a reference to Entity Framework version 5 or greater.'
---------------------------
OK   
---------------------------

Now suddenly my database model is broken (it just compiled earlier), claiming the following errors:

Error CS0246 The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?)

Error CS0234 The type or namespace name 'Entity' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)

Error CS0246 The type or namespace name 'DbSet<>' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'DbSet<>' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'DbSet<>' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'DbSet<>' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'DbSet<>' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'DbModelBuilder' could not be found (are you missing a using directive or an assembly reference?)

Error CS1729 'DbContext' does not contain a constructor that takes 1 arguments

I have created sites this way for 10 years, what has Microsoft broken now? More important, how do I fix it?

update: Comparing my packages.config, before and after, I notice that clicking "Add" in the wizard changes one line:

<package id="EntityFramework" version="6.4.4" targetFramework="net48" />

//changes to

<package id="EntityFramework" version="6.1.3" targetFramework="net48" />

So it's attempting to DOWNGRADE the Entity Framework from 6.4.4 to 6.1.3.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,253 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yihui Sun-MSFT 801 Reputation points
    2021-03-23T03:27:52.293+00:00

    Hi @Bryan Valencia ,

    According to the error message, you seem to have generated the entity and the context is still in EF version 5.

    You need to upgrade it to Entity Framework 6.

    1. Install the EF6 NuGet package
    2. Ensure that assembly references to System.Data.Entity.dll are removed
    3. Swap any EF Designer (EDMX) models to use EF 6.x code generation
    4. Update namespaces for any core EF types being used

    You can click this link to see a more detailed explanation.


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best Regards,
    YihuiSun

    1 person found this answer helpful.
    0 comments No comments