Search code examples
c#sql-serverentity-framework.net-6.0asp.net-core-2.0

Issue on migrating from .NET Core 2.2 to .NET 6


I recently migrated my app from .NET Core 2.2 to .NET 6 and upgraded the C# language from C#8 to C#10.

The app uses SQL Server and it's based on database first.

After migration, I noticed 2 changes:

  1. Entity Framework database first changes column mapping (column's names), Example:

    • The column name in the database: "Topic_Id",
    • The column name in the entity will change to: "TopicId" (Removing "_"):

    Is there a way that I have the exact column name in both the SQL server and Entity Framework?

    Please note I am generating the Entity using Scaffold-DbContext:

    PM> Scaffold-DbContext "Server=(servername);Database=(dbname);MultipleActiveResultSets=true;User ID=usr;Password=pass" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data/DB -force

  2. After moving to the .NET 6, I need to rewrite my queries - I need to convert them to IEnumerable.

    Is there a way that I avoid rewriting all queries?

    • In .NET Core 2.2:

      db.TblUserCredential UserCredential = this.DbContext.TblUserCredential
         .Where(x => x.CredentialTypeNavigation.Code.ToLower().Contains("kingsid"))
         .FirstOrDefault(x=> x.Identifier.Equals(identifier, StringComparison.OrdinalIgnoreCase));
      
    • In .NET 6:

      db.TblUserCredential? UserCredential = ((IEnumerable<db.TblUserCredential>)this.DbContext.TblUserCredentials.Include(x => x.CredentialTypeNavigation))
        .Where(x => x.CredentialTypeNavigation.Code != null && x.CredentialTypeNavigation.Code.ToLower().Contains("kingsid"))
        .FirstOrDefault(x => x.Identifier != null && x.Identifier.Equals(identifier, StringComparison.OrdinalIgnoreCase));         
      

am I missing anything? Any help would be appreciated


Solution

  • Example: The column name in the database: "Topic_Id", The column name in the entity will change to: "TopicId" (Removing "_"):

    Try specifying -UseDatabaseNames (or --use-database-names for .NET CLI) in the scaffold command - check out Preserving database names section of the scaffold docs.

    Is there a way that I avoid rewriting all queries?

    No. EF Core in 2nd version had a nasty little feature which was enabled by default - automatic client side evaluation for queries which it could not translate (i.e. it would silently fetch potentially excessive data and perform the rest of operations at the client-side). The following breaking change in EF Core 3 removed this feature and the only option to trigger client side evaluation is to convert query to IEnumerable (for example using AsENumerable). I highly recommend you to investigate the queries and rewrite them in a way so the can be translated completely.