有两种型号:
public class Course
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CourseId { get; set; }
[Required(ErrorMessage = "Обязательно для заполнения")]
[Display(Name = "Название курса")]
public string Title { get; set; }
[Required(ErrorMessage = "Обязательно для заполнения")]
[Display(Name = "Описание курса")]
public string Description { get; set; }
[Required(ErrorMessage = "Обязательно для заполнения")]
[Display(Name = "Стоимость курса")]
public decimal Price { get; set; }
//Configure 1 to many and NavigationProperty
//foreighn key
public int? CurrentTeacherId { get; set; }
public Teacher Teacher { get; set; }
}
public class Teacher
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TeacherId { get; set; }
[Required(ErrorMessage = "Обязательно для заполнения")]
[Display(Name = "Имя")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Обязательно для заполнения")]
[Display(Name = "Отчество")]
public string MiddleName { get; set; }
[Required(ErrorMessage = "Обязательно для заполнения")]
[Display(Name = "Фамилия")]
public string LastName { get; set; }
[Required(ErrorMessage = "Обязательно для заполнения")]
[Display(Name = "Опыт")]
public int Expirience { get; set; }
public List<Course> Courses { get; set; }
}
我已经实现了一个通用接口
public interface IRepository<T> where T:class
{
void Add(T item);
void Update(int id, T Item);
List<T> GetAllRecords();
T GetById(int id);
}
被覆盖的方法
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected DataContext Context;
public BaseRepository(DataContext dataContext)
{
Context = dataContext;
}
public void Add(TEntity item)
{
Context.Set<TEntity>().Add(item);
Context.SaveChanges();
}
public List<TEntity> GetAllRecords()
{
return Context.Set<TEntity>().ToList();
}
public TEntity GetById(int id)
{
return Context.Set<TEntity>().Find(id);
}
public void Update(TEntity Item)
{
Context.Entry(Item).State = EntityState.Modified;
Context.SaveChanges();
}
}
我创建了另一个类,在其中添加了 2 个接口(即ICourseRepository
,ITeacherRepository
我们使用其中的方法扩展我们的接口)。因此,类CourseRepository
public interface ICourseRepository : IRepository<Course>
{
//List<Course> GetCoursesByTeacherId(int teacherid);
}
public interface ITeacherRepository : IRepository<Teacher>
{
}
public class CourseRepository : BaseRepository<Course>, ICourseRepository
{
private readonly DataContext _dataContext;
private ICourseRepository _courseRepository;
//private readonly ITeacherREpository _teacherREpository;
//ITeacherREpository teacherREpository
public CourseRepository(DataContext dataContext, CourseRepository courseRepository) : base(dataContext)
{
_dataContext = dataContext;
_courseRepository = courseRepository;
//_teacherREpository = teacherREpository;
}
//public List<Course> GetCoursesByTeacherId(int teacherid)
//{
// Teacher teacher = _teacherREpository.Find(p => p.TeacherId == teacherid);
// if (teacher != null)
// {
// List<Course> asd = _dataContext.Courses.Where(p => p.Teacher.TeacherId == teacher.TeacherId).AsNoTracking().ToList();
// }
// throw new NotImplementedException();
//}
}
我现在如何使用控制器中方法的所有功能,并使用 Ninject。这就是我卡住的地方。我在控制器中写了以下内容。编码。
IRepository<Course> repository;
public HomeController(IRepository<Course> _repository)
{
repository = _repository;
IKernel kernel = new StandardKernel();
kernel.Bind<ICourseRepository>().To<CourseRepository>();
repository = kernel.Get<ICourseRepository>();
}
但它给出了一个错误。
问题是在 Global.Asax 文件中注册依赖项。正如 tym32167 回答的那样。(他们没有向我注册),通用接口的给定代码(如下)。我的行动:
在 global.asax 文件中
在控制器中: