Porting Entity Framework 6 to Entity Framework Core

cebuhax0r 66 Reputation points
2023-01-20T16:28:32.4533333+00:00

I am porting an application from ASP.net MVC 5 to ASP.net Core MVC and I reach out in the area pointing the Entity Framework related modules.

I have the generated classes and I also have the edmx and tt files, I added the files to a newly created Class Library (.net core) it seems like it needs to be regenerated (or im not sure), the edmx cannot be opened (and there are no errors),

I have tried to manualy update the derived models to use an updated libraries to use

"using Microsoft.EntityFrameworkCore;"

but i feel that these models needs to be regnerated instead of doing them manualy.

One thing I observe is it also finds for ConfigurationManager instead of the appsettings.json for .net core.

WHat is the proper way of doing this?

Summary

I have edmx and tt files, and i have the generated files as well, How can I use it for ASP.net Core MVC 6.0, does it need to be regenerated?

What is the proper way of doing this? (porting the entity framework 6 stuff to be used in ASP.NET Core MVC)

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
726 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,648 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2023-01-20T16:57:29.1833333+00:00

    edmx and tt files are not supported by EF core. you need to use ef core scaffolding instead, and regenerate the classes.

    try the documentation:

    [https://learn.microsoft.com/en-us/ef/efcore-and-ef6/porting/port-edmx

    0 comments No comments

  2. SurferOnWww 2,491 Reputation points
    2023-01-22T02:31:26.4833333+00:00

    What is the proper way of doing this?

    You will be able to use the context class and entity class generated by the reverse engineering. See the following documents for details:

    Reverse Engineering

    https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=dotnet-core-cli

    Scaffold-DbContext

    https://learn.microsoft.com/en-us/ef/core/cli/powershell#scaffold-dbcontext

    0 comments No comments

  3. SurferOnWww 2,491 Reputation points
    2023-01-22T02:32:23.42+00:00

    What is the proper way of doing this?

    You will be able to use the context class and entity class generated by the reverse engineering. See the following documents for details:

    Reverse Engineering

    https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=dotnet-core-cli

    Scaffold-DbContext

    https://learn.microsoft.com/en-us/ef/core/cli/powershell#scaffold-dbcontext

    0 comments No comments

  4. Karen Payne MVP 35,386 Reputation points
    2023-01-22T14:35:37.99+00:00

    One option is to use a Visual Studio extension EF Power Tools to reverse engineer your database which is easier than manually scaffolding your database.

    See my article on using EF Power Tools.

    Note that EF Power Tools will place the connection string in the DbContext which can be removed and placed into appsettings.json.

    Change the connection string

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*",
      "ConnectionStrings": {
        "DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MockupApplication;Integrated Security=True"
      
      }
    }
    

    In Program.cs

    var configuration = new ConfigurationBuilder()
        .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
        .AddJsonFile("appsettings.json")
        .Build();
    

    Then

    builder.Services.AddDbContextPool<Context>(options =>
        options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
    
    0 comments No comments