Entity Framework Core that creates a model from an existing database

With the Entity Framework Core, how do you create an EF model and entities?

According to ASP.NET Core - Existing Database In a Microsoft article, you need to run the following command in the package manager console:

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models 

This gives you zero control over which tables or views you want to import. Is it possible that this is the only way to reverse engineer the database and create EF models and objects now with EF Core and how is this progress compared to how it has been done with the full Entity Framework for many years?

+13
c # entity-framework
source share
6 answers

There is no way to do this in the Entity Framework Core. Read the documentation here: https://docs.microsoft.com/en-us/ef/efcore-and-ef6/features

+5
source share

I know this question is a bit outdated, but I think it is quite useful for people stumbling over the same issue.

If I understand your question correctly, you want to indicate which tables should be generated. This should be possible if you add the -Tables parameter to the command.

Here is the command I used to create 3 database tables (in the package manager console):

 Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context NorthwndContext -Tables Products,Categories,Suppliers -Force 

As you can see, I use the Northwnd database and only generate the Products, Categories, and Vendors tables. Obviously, you can add more tables, you just need to separate them with commas.

If you don’t know, you can get the DatabaseName by going to the Data Connections, click on the database you want to add, and on the right (Properties) you will see the (Name) property. For me it was "NORTHWND.MDF".

I used -Force to override any Models that I have already created.

You can use -DataAnnotations to get annotated models. Otherwise, you will receive a Fluent model configuration.

PS: I only tried this with ASP.NET Core 2 and Entity Framework Core 2.0.0.

+36
source share

My situation was that I had a .net 4.5+ class library with DbContexts.

Those DbContexts were created from an existing database using the "First code from an existing database" wizard. This Wizard seems to be missing from EF Core.

To create a new Code First DbContext from an existing database compatible with EF Core , I freely followed the guide here

My steps:

  • New kernel class library created

  • Added nuget package Microsoft.EntityFrameworkCore

  • Added nuget package Microsoft.EntityFrameworkCore.Tools
  • Added nuget package Microsoft.EntityFrameworkCore.SqlServer
  • Added nuget package Microsoft.EntityFrameworkCore.SqlServer.Design

  • Opened nuget package manager console

  • Entered a team

     Scaffold-DbContext "data source=MYSQLDBSERVER\MYSQLINSTANCE;initial catalog=MYDB;integrated security=True;MultipleActiveResultSets=True;" 
  • Introduced as provider

     Microsoft.EntityFrameworkCore.SqlServer 

Please note that when using a non-core project, you may encounter problems with the nuget package manager console. I avoided this problem by simply creating a new base class library, not .NET.

Once you have created the context, you can edit it, as usual, in Code First, for example. you can delete tables that you do not want to use.

+5
source share

You can also apply the cheater method: open VS2015, create a new lib class with the same name as the actual project, and then run the entity data model wizard.

Once you're done, copy and paste .net into your main project. It takes a little fix for the resulting code, but this is trivial.

However, executing the scaffold command as described above is the best solution.

+2
source share

I got this error when I first used the database:

Failed to load type 'Microsoft.EntityFrameworkCore.Infrastructure.DesignTimeProviderServicesAttribute' from the assembly 'Microsoft.EntityFrameworkCore, Version = 2.0.0.0, Culture = Neutral, PublicKeyToken = adb9793829ddae60'

There are 4 packages added:

 <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" /> 

and command:

 Scaffold-DbContext "Data Source=xxxx;Initial Catalog=xxx;UID=sa;PWD=xxx;MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entities/DB 
+1
source share

Those who want to convert the Sql database schema to the EF core using the dotnet core follow these steps:

Run all the commands one after another (the first command is for those who want to create a new project, otherwise you can ignore this and just run other commands from the root folder of your project)

 dotnet new mvc -o <ProjectName> dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design dotnet add package Microsoft.EntityFrameworkCore.Tools dotnet tool install --global dotnet-aspnet-codegenerator 

Finally...

 dotnet ef dbcontext scaffold "Server=servername\instancename;Database=My_Database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models 

This will create the necessary models and context of your database schema in the model folder of your project.

Now you can easily generate the CRUD code by applying the following command:

 dotnet aspnet-codegenerator controller -name <MyNewController> -m <ModelName> -dc <MyDbContext> --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries 

Change MyNewController to the desired controller name, ModelName with the model name in the model folder you want to target, and finally MyDbContext with the system-generated context file name available in the Models folder

but before running this command make sure that you have made the necessary changes to the appsettings.json and startup.cs files in your project folder

appsettings.json add after line

"AllowedHosts": "*",

  "ConnectionStrings": { "MyDbConnection": "Server=servername\\instancename;Database=My_Database;Trusted_Connection=True;" } 

file startup.cs add right before the line

. Services.AddMvc () SetCompatibilityVersion (CompatibilityVersion.Version_2_2);

 services.AddDbContext<SwiftRbs_LocalContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbConnection"))); 

Enjoy!!

0
source share

All Articles