由于某些原因,我的ApplicationDbContext为null

时间:2020-08-19 08:37:53

标签: asp.net-core razor-pages

我收到错误System.NullReferenceException。你调用的对象是空的。在Club.BLL.MaintenanceBLL.Maintenance.DoMaintenance()

但是上下文代码与脚手架页面上的代码相同,因此不确定我需要做什么。任何帮助表示赞赏。

代码是:

namespace Club.BLL.MaintenanceBLL

{   
    public class Maintenance
    {
        private readonly Club.Data.ApplicationDbContext Context; 

        public Maintenance(ApplicationDbContext context) 
     IN DEBUG MODE context IS INDICATED 
     AS OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT.  
     HOW DO I FIX THIS.
        {
            Context = context;
        }
        public Maintenance()
        {

        }
        
        public void DoMaintenance()
        {
            //Parse Maintenance table and action those items
            //where Active=True and ActionDate has passed
           
            //==================================
            //Retrieve list of rows in Maintenance table
            var maintenances = Context.Maintenance;  PROGRAM FAILS ON THIS LINE.

我想我在“边做边学”的过程中错过了一些基本知识。调用DoMaintenance例程的行位于root / Pages / Index,这是一个脚手架页面。在root / Pages / index.cshtml页面的以下几行中调用DoMaintenance例程:

     public void OnGet()
     {
        Maintenance maintenance = new Maintenance();//Create new instance
        maintenance.DoMaintenance();
     }

AND startup.cs包含以下行 公共无效ConfigureServices(IServiceCollection服务) { services.AddDbContext(options => options.UseSqlServer( Configuration.GetConnectionString(“ DefaultConnection”)))); services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) .AddRoles() .AddEntityFrameworkStores();

任何帮助都将得到认可。

谢谢和欢呼。。艾伦

2 个答案:

答案 0 :(得分:1)

根据您的描述和代码,我发现您已在应用程序中将dbcontext注册为服务,这意味着如果要使用它,则应将dbcontext注入到Maintenance类中。

通常,我们将在asp.net核心中将服务创建为BLL服务。

然后我们可以在startup.cs中注册该服务,并将其注入到剃须刀页面中。

我建议您可以尝试修改Maintenance类,如以下代码所示:

您可以创建一个名为IMaintenance的接口:

public  interface IMaintenance
{
    public void DoMaintenance();
}

然后您可以让Maintenance如下继承IMaintenance:

public class Maintenance : IMaintenance
{
    private readonly TestDbcontext Context;


    public Maintenance(TestDbcontext testDbcontext ) {
        Context = testDbcontext;
    }

    public void DoMaintenance() {

        var maintenances = Context.Employees.ToList() ;

        
    }
}

最后,您可以在startup.cs ConfigureServices方法中将维护作为服务注册:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddDbContext<TestDbcontext>(ServiceLifetime.Transient);


        services.AddTransient<IMaintenance, Maintenance>();

    }

您可以在如下所示的剃刀页面中直接使用“维护”,asp.net核心依赖项注入将自动将dbcontext注入“维护”中。

public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;
    private readonly IStringLocalizer<IndexModel> _localizer;
    private readonly IMaintenance _maintance;

    public CustomerModel Customer { get; set; }


    public IndexModel(ILogger<IndexModel> logger, IStringLocalizer<IndexModel> localizer, IMaintenance maintance)
    {

        _logger = logger;
        _localizer = localizer;
        _maintance = maintance;
    }

    public void OnGet()
    {
        _maintance.DoMaintenance()
    }
}

namespace Club.Pages
{
    [AllowAnonymous]
    public class IndexModel : PageModel
    {
         private readonly IMaintenance  _maintenance;

        public IndexModel (IMaintenance<IndexModel> maintenance)  
        {
            _maintenance = maintenance;
        }

        public void OnGet()
        {
           _maintenance.DoMaintenance();
        }
    }
}

答案 1 :(得分:0)

您可能已经错过了注入上下文的功能,并且由于有默认的ctor,因此在不设置Context属性的情况下一切正常。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<Context>(() => ...));
    }
相关问题