less than 1 minute read

  1. Starting the project

https://docs.microsoft.com/ko-kr/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.0&tabs=visual-studio

  1. git init Run git init inside the project folder.

  2. Connecting the DB

# https://docs.microsoft.com/ko-kr/ef/core/get-started/?tabs=visual-studio#install-entity-framework-core
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.0.0

For appsettings.json, check connectionstrings.com

  1. Adding the model class

Add each column’s information.

using System.ComponentModel.DataAnnotations;

public class VisitorCode
{
    [Key]
    public string V_CODE { get; set; }
    public string V_CODETYPE { get; set; }
    public string V_CODENAME { get; set; }
}
  1. DbContext
public class VisitorContext : DbContext
{
    public DbSet<VisitorContext> VisitorCodes { get; set; }

    public VisitorContext(DbContextOptions<VisitorContext> options)
        : base(options)
    {
    }
}
  1. Registering in Startup
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddDbContext<VisitorContext>(options =>
        options.UseSqlServer("Server=164.246.66.33,1433;Database=Haksa;Trusted_Connection=True;User Id=sa;Password=dlgkrwh"));
}

The default MSSQL port is 1433.

  1. Controller

Right-click the Controllers folder. Select Add > New Scaffolded Item. Select “API Controller with actions, using Entity Framework” and select Add. In the “Add API Controller with actions, using Entity Framework” dialog: In Model class, select TodoItem (TodoApi.Models). In Data context class, select TodoContext (TodoApi.Models). Select Add.


Tags:

Categories:

Updated:

Leave a comment