Context is null in Data Access Layer when using n-tier architecture in Web API, Entity Framework

I'm trying to create a Web API. I'm using Entity Framework here, code first approach.

This is a n-tier architecture. Meaning, I have created multiple projects in a single solution as shown below.

enter image description here

I have a controller method which is a get method. I will pass userid as the parameter.

[Route("api/[controller]")]
[ApiController]
public class BankController : ControllerBase
{
    private readonly IBankApplicationDAL _bankAPI;

    public BankController(IBankApplicationDAL bankAPI)
    {
        _bankAPI = bankAPI;
    }

    [HttpGet("{userId}")]
    public IActionResult GetAccountDetailsId(int userId)
    {
        try
        {
            var values = _bankAPI.GetAccountDetailsId(userId);
            return Ok(values);
        }
        catch
        {
            throw;
        }
    }
}

In the DAL layer, this is what I have:

using BankApplicationAPI.Model;
using BankApplicationAPI.Data;
using System;
using System.Linq;

namespace BankApplicationAPI.DAL
{
    public class BankApplicationDAL : IBankApplicationDAL
    {
        DataContext context;

        public AccountDetails GetAccountDetailsId(int userId)
        {
            try
            {
                var values = context.AccountDetails.FirstOrDefault(x => x.UserId == userId); // context is null here.
                return values;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}

This is the DataContext I have:

namespace BankApplicationAPI.Data
{
    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options) { }

        public DbSet<AccountDetails> AccountDetails { get; set; }
        public DbSet<TransactionDetails> TransactionDetails { get; set; }
    }
}

When I run this application, I get an exception saying context is null in DAL method (where I have put comment).

How do I create an object for DataContext? Can anyone help me here? It's very important. Thank you very much.

EDIT

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    var connection = @"Server=.\sqlexpress;Database=AppDB;Trusted_Connection=True;ConnectRetryCount=0";

    services.AddDbContext<DataContext>(x => x.UseSqlServer(connection));
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddSingleton<IBankApplicationDAL, BankApplicationDAL>();
}

1 answer

  • answered 2019-04-15 05:52 hassan.ef

    you should use this changing:

     public class BankApplicationDAL : IBankApplicationDAL
     {
                DataContext _context;
                public BankApplicationDAL(DataContext context)
                {
                    _context = context;
                }
    
                public AccountDetails GetAccountDetailsId(int userId)
                {
                    try
                    {
                        var values = context.AccountDetails.FirstOrDefault(x => x.UserId == userId); // context is null here.
                        return values;
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                }
       }