User Login

Please enter user name and password into following boxes!

If you are not registered yet, please click

Forget Password

If you forget your password, please click
favorite

.NET Core DbContext Usage

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();
}


PayPal - The safer, easier way to pay online!


Comments About Tutorial

There is no comment



Add Comment

Name*
Surname* 
Email* 
Subject* 
What is (12 + 5) :
WARNING

Please fill all text boxes

Message Result

Your comment is saved. After approved, it will be published

An Error Occured

We are sorry, your request cannot be continued try again later

LOGIN REQUESTED

Please login for this operation
Login