简体   繁体   English

在Web API,实体框架中使用n层体系结构时,数据访问层中的上下文为null

[英]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. 我正在尝试创建一个Web API。 I'm using Entity Framework here, code first approach. 我在这里使用实体框架,代码优先方法。

This is a n-tier architecture. 这是n层架构。 Meaning, I have created multiple projects in a single solution as shown below. 意思是,我在一个解决方案中创建了多个项目,如下所示。

在此处输入图片说明

I have a controller method which is a get method. 我有一个控制器方法,这是一个get方法。 I will pass userid as the parameter. 我将通过userid作为参数。

[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: 在DAL层中,这就是我所拥有的:

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: 这是我拥有的DataContext

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). 当我运行该应用程序时,出现一个异常,说明DAL方法(在其中已添加注释)的上下文为null。

How do I create an object for DataContext ? 如何为DataContext创建对象? Can anyone help me here? 有人能帮我一下吗? It's very important. 这很重要。 Thank you very much. 非常感谢你。

EDIT 编辑

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有服务层,业务层和实体框架的N层体系结构 - N-Tier Architecture with Service Layer, Business Layer, and Entity Framework 实体框架在N层架构中不起作用 - Entity Framework is not working in N-Tier Architecture 具有n层架构的实体框架5 - Entity Framework 5 with n-Tier Architecture 在实体框架中使用存储过程进行n层数据同步 - Using stored procedure in entity framework for n-tier data synchronisation 使用实体框架5代码优先和POCO实现N层数据层? - N-Tier Data Layer implementation with Entity Framework 5 Code-First and POCOs? 如何在n层体系结构中将Entity Framework模型类与Business Layer类映射 - ASP.NET-MVC - How to map Entity Framework model classes with Business Layer class in n-tier architecture - ASP.NET-MVC 使用N层实体框架,如何访问已标记为已删除的实体? - Using N-Tier Entity Framework, how can I access an entity that has been marked as deleted? C#中的实体和N层体系结构 - Entity and N-Tier architecture in C# 在N层实体框架中使用存储过程 - Using Stored Procedures with N-Tier Entity Framework 使用N层实体框架在WinForms中显示数据注释错误消息 - Displaying Data Annotation Error Messages in WinForms Using N-Tier Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM