Skip to content

A scalable .NET Solutions Architecture (ASP.NET Core + EF Core) code-first database example #RESTful microservices #loose-coupling #ci-cd. cloud data warehouse/lakehouse (Microsoft Fabric)

Notifications You must be signed in to change notification settings

i-krishna/aspnetcore-efcore-db-webapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aspnetcore-efcore-db-webapi

A scalable .NET Solutions Architecture (ASP.NET Core + EF Core) code-first database example, where C# classes define the database schema. EF Core tracks schema changes via migrations for version control. This is designed with enterprise patterns (SOLID, Clean Architecture). Demonstrates:

  • SQLServer/EF Core migrations
  • RESTful API best practices
  • CI/CD-ready structure
  1. Project Setup Create a new ASP.NET Core Web API project:
dotnet new webapi -n EFCoreDemo
cd EFCoreDemo
  1. Install EF Core Packages
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer  # For SQL Server
  1. File Structure of defining model in Product.cs, Creating DbContext in AppDbContext.cs & Configuring Dbcontext in Program.cs
aspnetcore-efcore-db-webapi/
├── Models/
│   └── Product.cs
├── Data/
│   └── AppDbContext.cs
├── appsettings.json
├── Program.cs
└── aspnetcore-efcore-db-webapi.csproj
  1. Run the Project
  • Apply migrations : Migrations translate C# model classes (Product.cs) into actual database tables (e.g., a Products table in SQL Server). Also, track changes to schema (versioning).

Example:

public class Sales
{
    public int Id { get; set; }
    public string Product { get; set; }
    public decimal Amount { get; set; }
    // Add new properties as needed
}
  • Add a property or new class, then run migration command.
# Using Package Manager Console
Add-Migration AddCustomerField

# Or using CLI
dotnet ef migrations add AddCustomerField
dotnet ef migrations add InitialCreate
  • Apply the migration to update the database:
# Using Package Manager Console
Update-Database

# Or using CLI
dotnet ef database update
  • Entity Framework generates a migration file (C#) like:
public partial class AddCustomerField : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<string>(
            name: "Customer",
            table: "Sales",
            nullable: true);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "Customer",
            table: "Sales");
    }
}
  • EF tracks schema changes in a migration history table (__EFMigrationsHistory).

  • This creates an app.db SQLite file and the Migrations/ folder

b. Start the API:

dotnet run

c. Test the endpoint:

GET https://localhost:5001/api/products Returns products.

POST https://localhost:5001/api/products Adds a new product (send JSON body).

[
  { "id": 1, "name": "P1", "price": 49.99 },
  { "id": 2, "name": "P2", "price": 199.99 }
]

Refereces

Agile Software development

The Bible of Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. http://wwwswt.informatik.uni-rostock.de/patterns/index.html https://www.edx.org/course/agile-software-development-ethx-asd-1x (scrum)

Microservices architectures

Continuous Integration, Deployment & Delivery

Maven, Git, GitLab, jUnit, Jenkins https://codeship.com/continuous-integration-essentials http://www.vogella.com/tutorials/Jenkins/article.html http://www.guru99.com/test-driven-development.html

About

A scalable .NET Solutions Architecture (ASP.NET Core + EF Core) code-first database example #RESTful microservices #loose-coupling #ci-cd. cloud data warehouse/lakehouse (Microsoft Fabric)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages