IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Entity Framework Core, code first | Entity Framework Core Model First | |
🗄️ Entity Framework Core - Database First Approach |
Database First is an approach in EF Core where the database schema already exists, and EF Core generates the DbContext and entity classes from that schema. This is useful when working with legacy databases or when the database is designed and maintained by DBAs.
Steps to implement Database First in EF Core:
Microsoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.Tools
Scaffold-DbContext "Server=.;Database=DemoDb;Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
This generates entity classes and a DbContext based on the database schema.
// Example entity generated from database
public partial class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
// Example DbContext
public partial class DemoDbContext : DbContext
{
public virtual DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=.;Database=DemoDb;Trusted_Connection=True;");
}
}
EF Core Database First is best when working with an existing database schema. It allows developers to scaffold models and DbContext directly from the database, making it ideal for legacy systems or DBA-driven environments. While it lacks the flexibility of Code First migrations, it ensures tight alignment with the database design.
| Entity Framework Core, code first | Entity Framework Core Model First | |