Skip to content

Instantly share code, notes, and snippets.

View subhendu-de's full-sized avatar
👓
Coder by Choice

Subhendu De subhendu-de

👓
Coder by Choice
View GitHub Profile
mkdir eKart
cd eKart
REM creates an empty solution
dotnet new sln --name eKart
REM creates a folder for .NET Core 2.1
mkdir netcore21
cd netcore21
mkdir src
cd src
REM creates an ASP.NET Core web api project
namespace eKart.Api.Model
{
public class Product
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public int ID { get; set; }
public string Name {get; set; }
}
}
using System;
using Microsoft.EntityFrameworkCore;
namespace eKart.Api.Model
{
public interface IDatasourceContext: IDisposable
{
DbSet Products{ get; set; }
int SaveChanges();
}
using System;
using Microsoft.EntityFrameworkCore;
namespace eKart.Api.Model
{
public class SQLServerContext: DbContext, IDatasourceContext
{
public SQLServerContext(DbContextOptions options):base(options)
{}
public DbSet Products{get; set; }
using eKart.Api.Model;
using Microsoft.EntityFrameworkCore;
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IDatasourceContext, SQLServerContext>();
services.AddDbContext(options => options.UseSqlServer(@"Server=.;Database=eKart;Trusted_Connection=True;ConnectRetryCount=0"));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
dotnet ef migrations add InitialCreate
dotnet ef database update
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using eKart.Api.Model;
namespace eKart.Api.Controllers
{
[Route("api/[controller]")]
dotnet add package Swashbuckle.AspNetCore
using Swashbuckle.AspNetCore.Swagger;
namespace eKart.Api
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Register the Swagger generator, defining 1 or more Swagger documents
dotnet run --project .\netcore21\src\eKart.Api\eKart.Api.csproj
dotnet run --project .\netcore21\src\eKart.Api\eKart.Api.csproj