Entity Framework 使用 Autofac IOC 时数据库上下文为空
Entity Framework DB Context null when using Autofac IOC
我正在尝试将查询逻辑移出我的控制器。我的问题是上下文为空,当我尝试获取宠物列表时,在我的具体 class PetRepository 中抛出异常。
在界面中:
public interface IPetRepository
{
List<Pet> GetAllPets();
PetStoreContext context { get; set; }
}
具体实现中:
public class PetRepository : IPetRepository
{
public PetStoreContext context { get; set; }
public List<Pet> GetAllPets()
{
return context.Pet.ToList(); //this line throws a null exception
}
}
在我的控制器中,我正在使用构造函数注入:
public class PetsController : BaseController
{
private IPetRepository _petRepository;
public PetsController(IPetRepository petRepository)
{
_petRepository = petRepository;
}
}
然后是我的操作结果
public ActionResult Index()
{
var model = new PetListing()
{
Pets = _petRepository.GetAllPets()
}
return View(model);
}
最后我只是用 autofac 做了一个简单的映射。
private void RegisterAutoFac()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterSource(new ViewRegistrationSource());
builder.RegisterType<PetRepository>().As<IPetRepository>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
如果我直接在我的控制器中使用相同的代码行,那么我会从数据库中得到一份宠物列表,例如
public ActionResult Index()
{
public PetStoreContext context = new PetStoreContext();
return View(context.Pet.ToList());
}
context
是一个 属性 没有被设置,而是让 IoC 通过将它注入你的 repo 来为你实例化它:
public class PetRepository : IPetRepository
{
private readonly PetStoreContext _context;
public PetRepository(PetStoreContext context)
{
_context = context;
}
public List<Pet> GetAllPets()
{
return _context.Pet.ToList();
}
}
我正在尝试将查询逻辑移出我的控制器。我的问题是上下文为空,当我尝试获取宠物列表时,在我的具体 class PetRepository 中抛出异常。
在界面中:
public interface IPetRepository
{
List<Pet> GetAllPets();
PetStoreContext context { get; set; }
}
具体实现中:
public class PetRepository : IPetRepository
{
public PetStoreContext context { get; set; }
public List<Pet> GetAllPets()
{
return context.Pet.ToList(); //this line throws a null exception
}
}
在我的控制器中,我正在使用构造函数注入:
public class PetsController : BaseController
{
private IPetRepository _petRepository;
public PetsController(IPetRepository petRepository)
{
_petRepository = petRepository;
}
}
然后是我的操作结果
public ActionResult Index()
{
var model = new PetListing()
{
Pets = _petRepository.GetAllPets()
}
return View(model);
}
最后我只是用 autofac 做了一个简单的映射。
private void RegisterAutoFac()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterSource(new ViewRegistrationSource());
builder.RegisterType<PetRepository>().As<IPetRepository>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
如果我直接在我的控制器中使用相同的代码行,那么我会从数据库中得到一份宠物列表,例如
public ActionResult Index()
{
public PetStoreContext context = new PetStoreContext();
return View(context.Pet.ToList());
}
context
是一个 属性 没有被设置,而是让 IoC 通过将它注入你的 repo 来为你实例化它:
public class PetRepository : IPetRepository
{
private readonly PetStoreContext _context;
public PetRepository(PetStoreContext context)
{
_context = context;
}
public List<Pet> GetAllPets()
{
return _context.Pet.ToList();
}
}