.NET Core DbContext Usage

14-07-2017
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using VirtualPos.Models;

namespace VirtualPos.Controllers
{
    [Route("api/[controller]")]
    public class BankController : Controller
    {
        private readonly BankContext _context;
        private readonly IStringLocalizer<BankController> _localizer;

        public BankController(BankContext context, IStringLocalizer<BankController> localizer)
        {
            _context = context;
            _localizer = localizer;
            if (!_context.Companies.Any())
            {
                _context.Companies.Add(new Company {Id = 1});
                _context.SaveChanges();
            }
        }

        [HttpGet]
        public IEnumerable<Company> GetAll()
        {
            return _context.Companies.ToList();
        }

        [HttpGet("sonuc/{id}", Name = "GetTodo")]
        public IActionResult GetById(long id)
        {
            var item = _context.Companies.FirstOrDefault(t => t.Id == id);
            if (item == null)
                return NotFound();

            return new ObjectResult(_localizer["Hello"]);
        }

        [HttpPost]
        public IActionResult Create([FromBody] Company item)
        {
            if (item == null)
                return BadRequest();

            _context.Companies.Add(item);
            _context.SaveChanges();

            return CreatedAtRoute("GetTodo", new {id = item.Id}, item);
        }

        [HttpPut("{id}")]
        public IActionResult Update(long id, [FromBody] TodoItem item)
        {
            if (item == null || item.Id != id)
                return BadRequest();

            var todo = _context.Companies.FirstOrDefault(t => t.Id == id);
            if (todo == null)
                return NotFound();
            _context.Companies.Update(todo);
            _context.SaveChanges();
            return new NoContentResult();
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(long id)
        {
            var todo = _context.Companies.First(t => t.Id == id);
            if (todo == null)
                return NotFound();

            _context.Companies.Remove(todo);
            _context.SaveChanges();
            return new NoContentResult();
        }
    }
}

Model Class

namespace VirtualPos.Models
{
    public class Company
    {
        public int Id { get; set; }
        public string ClientId { get; set; }
        public string StoreKey { get; set; }
        public string StoreType { get; set; }
       
        public string Username { get; set; }
        public string Password { get; set; }
    }
}

Context Class

using Microsoft.EntityFrameworkCore;

namespace VirtualPos.Models
{
    public class BankContext : DbContext
    {
        public BankContext(DbContextOptions<BankContext> options)
        : base(options) {}
        
        public DbSet<Company> Companies { get; set; }
        
    }
}

Startup.cs Class

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<BankContext>(opt => opt.UseInMemoryDatabase());
    services.AddMvc().AddViewLocalization(
    LanguageViewLocationExpanderFormat.Suffix,
    opts => { opts.ResourcesPath = "Resources"; })
    .AddDataAnnotationsLocalization();
}

© 2019 All rights reserved. Codesenior.COM