Afshar
Afshar

Reputation: 11523

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.

Upvotes: 10

Views: 11274

Answers (4)

ram
ram

Reputation: 21

Code First/DB First titles are a bit misleading, but if you're looking for an EDMX based solution, then there's no support in EF Core since EF Core was a completely redesigned ORM.

Instead you can use database scaffolding to create context classes based on your existing database.

Whenever the database changes, you'd simply run the same again so that the existing context classes and their definitions get overwritten by the updated database.

Sounds like a long process, but if you're working on a layered architecture with separated contracts layers, its a lot easier than you think.

https://referbruv.com/blog/posts/database-first-generating-models-from-an-existing-database-using-ef-core

Upvotes: -1

Klamsi
Klamsi

Reputation: 906

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.

Upvotes: 1

Asherguru
Asherguru

Reputation: 1741

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

Upvotes: 18

ErikEJ
ErikEJ

Reputation: 41799

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.

Upvotes: 5

Related Questions