- Create the solution: dotnet new sln -n MySolution
- Create the ASP.NET Core Web API project: dotnet new webapi -n MyAPI
- Create the xUnit Test Project: dotnet new xunit -n MyAPI.Tests
- Add projects into the solution:
- dotnet sln MySolution.sln add MyAPI/MyAPI.csproj
- dotnet sln MySolution.sln add MyAPI.Tests/MyAPI.Tests.csproj
- Add API reference into the xUnit project:
- dotnet add MyAPI.Tests/MyAPI.Tests.csproj reference MyAPI/MyAPI.csproj
ASP.NET already includes the Microsoft.Extensions.Logging
To use the Serilog, add the following packages:
- dotnet add MyAPI package Serilog.AspNetCore
- dotnet add MyAPI package Serilog.Sinks.Console
- dotnet add MyApi package Microsoft.EntityFrameworkCore.Sqlite
- dotnet add MyApi package Microsoft.EntityFrameworkCore.Design
- dotnet add MyAPI package Microsoft.AspNetCore.Mvc.Core
Add into MyAPI Program.cs or Startup.cs
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlite("Data Source=app.db"));Create the AppDbContext:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<MyEntity> MyEntities { get; set; }
}
public class MyEntity
{
public int Id { get; set; }
public string Name { get; set; }
}Run EF Core migrations
- dotnet ef migrations add InitialCreate --project MyApi
- dotnet ef database update --project MyApi
Install the CLI program
- dotnet tool install --global dotnet-ef