简体   繁体   中英

DbContext Issues Upgrading from .NET Core 1 to .NET 5

I have an older program running on .NET Core 1 and want to get it working with .NET 5. Instead of going through the tedious process of upgrading each version step by step I decided to just create a new solution and start from scratch. Doing this, I am able to copy most files from the older projects.

My issue is in the DbContext classes.

public partial class ClassName : DbContext

There are a number of errors throughout, but each give me the same (or similar) suggestion to resolve them, relating to a missing using reference.

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

Now, I know I am missing the using Microsoft.EntityFrameworkCore; reference. So when I add that package to the project, my file changes and I now get about 500 errors (about half the lines in the file).

Some examples of these new errors are as follows:

entity.HasKey(e => e.Id)
  .HasName("PK");
    
entity.ToTable("TB");

In the above snippet, .HasName is throwing the error 'KeyBuilder' does not contain a definition for 'HasName' and no accessible extension method 'HasName' accepting a first argument of type 'KeyBuilder'.

As someone new to entity framework, am I missing something obvious or should I be re-scaffolding the database to re-create these context files?

No answer here yet so someone might miss that the comments actually holds an answer.

As @Kirk Larkin states in a comment to the question the solution is to add the Microsoft.EntityFrameworkCore.Relational package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM