Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does EF Core 3.1 support DB First approach?

We are porting an ASP.NET MVC 4.x application to ASP.NET Core 3.1. The current application is using EF 6.x DB first approach. As a part of this migration we are going to use EF Core 3.1 as an alternative to the current EF 6.x. So the question is:

Does EF Core 3.1 support DB First approach?

If not, what are the options? Are we left with only code first approach?

Appreciate your helps.

like image 280
Afshar Mohebi Avatar asked Jan 19 '20 13:01

Afshar Mohebi


People also ask

Is scaffold-DbContext command is used in EF code First approach?

The EF core only supports Code First & Database First approach. In Database First, We use the Scaffold-dbcontext to create the Model from an existing database. This is basically Reverse engineering the existing database. Once we create the entity classes databases first does not work.

What databases does the Entity Framework core support?

EF Core works with many databases, including SQL Database (on-premises and Azure), SQLite, MySQL, PostgreSQL, and Azure Cosmos DB.

Can Core 3.1 Use EF Core 5?

EF Core 5.0 requires a . NET Standard 2.1 platform. This means EF Core 5.0 will run on . NET Core 3.1 or .

What is database first approach in Entity Framework?

The Database First Approach provides an alternative to the Code First and Model First approaches to the Entity Data Model. It creates model codes (classes, properties, DbContext etc.) from the database in the project and those classes become the link between the database and controller.

Is there a DB first approach in EF Core?

Yes, EF Core supports database first via the Scaffold-DbContext command, and you can also use EF Core Power Tools. Edmx based modelling is not available with EF Core, only code based modelling. No, unfortunately there is no DB first approach anymore. What you can do is code first from existing database.

Does Entity Framework Core support database-first approach?

Entity Framework Core supports Database-First approach via the Scaffold-DbContext command of Package Manager Console. This command scaffolds a DbContext and entity type classes for a specified database.

Which database provider should I use with EF Core?

For example, a provider released for EF Core 2.1 should work with EF Core 2.2, but will not work with EF Core 3.0. Most database providers for EF Core are distributed as NuGet packages, and can be installed as follows:

What is EF Core and how can it be extended?

Database providers can extend EF Core to enable functionality unique to specific databases. Some concepts are common to most databases, and are included in the primary EF Core components. Such concepts include expressing queries in LINQ, transactions, and tracking changes to objects once they are loaded from the database.


3 Answers

Yes. It supports DB First Approach since .NET Core 1.0 until now. You need to download 4 from nugets

  1. EntityFrameworkCore

  2. EntityFrameworkCore.Design

  3. EntityFrameworkCore.Tools

  4. EntityFrameworkCore.SqlServer

Open Tools > NuGet Package Manager > Package Manager Console. And enter this below in console.

Default:

Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context "YourOwnContext"

Saw your comment about "Scaffold-DbContext only creates a Code First model". No, Scaffold-DbContext is Database-First approach.

"Creating entity & context classes for an existing database is called Database-First approach."

EDITED

If you have new update in database and want to update dbcontext, just add -f at end. It will update and overwrite all your dbcontext and model classes.

Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context "YourOwnContext" -f

If you want to add some data annotations such as [Column], etc in model class, can add -DataAnnotations

Scaffold-DbContext "Server=yourserveraddress;Database=yourdatabase;user id=youruser;password=yourpassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context "YourOwnContext" -DataAnnotations -f
like image 115
Asherguru Avatar answered Nov 10 '22 12:11

Asherguru


Yes, EF Core supports database first via the Scaffold-DbContext command, and you can also use EF Core Power Tools. Edmx based modelling is not available with EF Core, only code based modelling.

like image 44
ErikEJ Avatar answered Nov 10 '22 12:11

ErikEJ


No, unfortunately there is no DB first approach anymore.

What you can do is code first from existing database. With the scaffold command that was already mentioned.

What we do:

  • Use scaffold
  • Create "scaffold scripts" that control the scaffold process. (For example split the tables into more than one context)
  • Create "after scaffold scripts" that repair all mistakes scaffold makes every time. (for example broken data types)
  • Create more "after scaffold scripts" that enhance your models with all scaffold does not support. (for example column comments)
  • Use partial classes as much as possible to avoid losing changes every time

But in summary there is no satisfactory solution for that. Be aware that if you want a perfect db first approach you almost rewrite the whole scaffold functionality.

like image 38
Klamsi Avatar answered Nov 10 '22 14:11

Klamsi