Skip to content

dotnet-studies/ASP.NET-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Generate an ASP.NET project from .NET CLI


Create Solution, Project and Tests

  1. Create the solution: dotnet new sln -n MySolution
  2. Create the ASP.NET Core Web API project: dotnet new webapi -n MyAPI
  3. Create the xUnit Test Project: dotnet new xunit -n MyAPI.Tests
  4. Add projects into the solution:
    1. dotnet sln MySolution.sln add MyAPI/MyAPI.csproj
    2. dotnet sln MySolution.sln add MyAPI.Tests/MyAPI.Tests.csproj
  5. Add API reference into the xUnit project:
    1. dotnet add MyAPI.Tests/MyAPI.Tests.csproj reference MyAPI/MyAPI.csproj

Add NuGet Packages

Logging

ASP.NET already includes the Microsoft.Extensions.Logging

To use the Serilog, add the following packages:

  1. dotnet add MyAPI package Serilog.AspNetCore
  2. dotnet add MyAPI package Serilog.Sinks.Console

SQLite and EF Core

  1. dotnet add MyApi package Microsoft.EntityFrameworkCore.Sqlite
  2. dotnet add MyApi package Microsoft.EntityFrameworkCore.Design

Controller Support

  1. dotnet add MyAPI package Microsoft.AspNetCore.Mvc.Core

EF Core configuration to SQLITE

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

  1. dotnet ef migrations add InitialCreate --project MyApi
  2. dotnet ef database update --project MyApi

Install the CLI program

  1. dotnet tool install --global dotnet-ef

About

Create an ASP.NET project from .NET CLI

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •