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.
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.
EF Core works with many databases, including SQL Database (on-premises and Azure), SQLite, MySQL, PostgreSQL, and Azure Cosmos DB.
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 .
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.
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.
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.
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:
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.
Yes. It supports DB First Approach since .NET Core 1.0 until now. You need to download 4 from nugets
EntityFrameworkCore
EntityFrameworkCore.Design
EntityFrameworkCore.Tools
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
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. With the scaffold command that was already mentioned.
What we do:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With