0

在这里,在 Asp.Net 中,普通的 web 表单中,我使用依赖注入来检索记录。我也在这里使用了实体框架

我有一个类和一个界面如下所示..

public partial class Student
    {
        public Student()
        {

        }

        public Student StudentID { get; set; }
        public string StudentName { get; set; }
        public string Address { get; set; }
    }

public interface IStudentService
    {
        Student GetStudentsById(Student StudentID);
        IList<Student> GetAllStudents();
    }

创建了一个上下文类

public partial class Entities : DbContext
    {
        public DbSet<Student> Students { get; set; }
    }

然后实现接口

public partial class StudentService : IStudentService
    {
        Entities db = new Entities();

        public virtual Student GetStudentsById(Student StudentID)
        {
            //need to implement
        }    

        public virtual IList<Student> GetAllStudents()
        {
            //need to implement
        }    
    }

现在,谁能帮我实现这些方法

提前致谢

4

1 回答 1

3

假设public Student StudentID应该是public int StudentID

public partial class StudentService : IStudentService
{
    Entities db = new Entities();

    public virtual Student GetStudentById(int studentId)
    {
        return db.SingleOrDefault(s => s.StudentID == studentId);
    }

    public virtual IList<Student> GetAllStudents()
    {
        return db.Students.ToList();
    }
}
于 2012-10-08T13:54:15.180 回答