【发布时间】:2015-03-06 11:45:10
【问题描述】:
在控制器中,我试图获得“等级”,使用:
Grade g = gradeRepository.FindById(1);
findById 在 GradeRepository 中声明:
public class GradeRepository : IGradeRepository
{
private ProjectContext context;
private DbSet<Grade> grades;
public GradeRepository(ProjectContext context)
{
this.context = context;
grades = context.Grades;
}
public IQueryable<Grade> FindAll()
{
return grades.OrderBy(g => g.name);
}
public Grade FindById(int gradeId)
{
return grades.Include(l => l.DeterminateTableProp).FirstOrDefault(g => g.GradeInt == gradeId);
}
public void Remove(Grade grade)
{
grades.Remove(grade);
}
public void SaveChanges()
{
context.SaveChanges();
}
}
服务在 nijectwebcommon 文件中正确注册:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IContinentRepository>().To<ContinentRepository>().InRequestScope();
kernel.Bind<IGradeRepository>().To<GradeRepository>().InRequestScope();
kernel.Bind<ProjectContext>().ToSelf().InSingletonScope();
}
这可能是什么问题? 我所做的与我正在使用的另一个存储库完全相同。
【问题讨论】:
-
在代码中运行
Grade g = gradeRepository.FindById(1);会发生什么? -
它抛出一个异常(被捕获并重定向到另一个页面)。
-
异常类型是什么?留言?
-
SqlException。 EntityFramework.SqlServer.dll 中发生“System.Reflection.TargetInvocationException”类型的异常,但未在用户代码中处理附加信息:调用的目标已导致异常。
标签: c# visual-studio