C# 非静态字段、方法或属性需要对象引用';ApplicationDbContext.Locations';

C# 非静态字段、方法或属性需要对象引用';ApplicationDbContext.Locations';,c#,visual-studio,entity-framework,asp.net-mvc-5,C#,Visual Studio,Entity Framework,Asp.net Mvc 5,这是在我的控制器中,我有位于两个位置之一的患者,我正在尝试创建一个dropdownlist以在两个位置之间进行过滤 这就是我正在使用的模型 public ActionResult Filter() { var viewModel = new Location(); viewModel.Patients = ApplicationDbContext.Locations.ToList(); return View(viewModel);

这是在我的控制器中,我有位于两个位置之一的患者,我正在尝试创建一个dropdownlist以在两个位置之间进行过滤

这就是我正在使用的模型

 public ActionResult Filter()
    {
        var viewModel = new Location();
        viewModel.Patients = ApplicationDbContext.Locations.ToList();
        return View(viewModel);
    }
namespace FolderSystem.Models
{
公共类位置
{
公共int Id{get;set;}
公共字符串名称{get;set;}
[显示(Name=“Address”)]
public int?AddressId{get;set;}
公共虚拟ICollection患者{get;set;}
公共虚拟ICollection应用程序用户{get;set;}
公共虚拟地址{get;set;}
}
}

您必须将
ApplicationDbContext
中的对象声明为

namespace FolderSystem.Models
{
    public class Location
    {
        public int Id { get; set; }
        public string Name { get; set; }
        [Display(Name = "Address")]
        public int? AddressId { get; set; }
        public virtual ICollection<Patient> Patients { get; set; }
        public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }
        public virtual Address Address { get; set; }
    }
}
然后可以使用db而不是ApplicationDbContext,如下所示

ApplicationDbContext db = new ApplicationDbContext();

什么是ApplicationDbContext?这是一个字段或属性,还是您没有显示整个代码?这个字段/属性/变量是如何分配的?在我的数据文件夹中,我有applicationAndBContext.cs,还有“public DbSet Locations{get;set;}”,这就是你要问的吗?我将ApplicationDbContext设置为一个类,您需要后退一步,了解类型和实例之间的区别。这是基本的编程,有很多关于这方面的教程。基于你的下一个问题,我还建议你学习一些关于基本编程的教程。通过在中发布IDE中的错误来学习如何编程不是一个好方法。谢谢,但现在我遇到了错误,无法将类型“System.Collections.Gemeric.List”隐式转换为“System.Collections.Generic.ICollection”。存在显式转换(是否缺少转换?)?或者改变你的文件夹?我想我需要以某种方式将其转换为列表?但是我不确定我的朋友,我建议你先学习面向对象,不要面对这个问题。这将解决公共ActionResult筛选器(){var locations=db.locations.ToList();返回视图(位置);
public ActionResult Filter()
{
    var viewModel = new Location();
    viewModel.Patients = db.Locations.ToList();
    return View(viewModel);
}