如何加入ADO.NET实体框架

时间:2012-01-04 11:34:02

标签: c# asp.net entity-framework ado.net

我是Entity Framework的新手。我有一个如下所示的模型。

enter image description here

由于我需要从Silverlight访问它,我添加了DomainService

[EnableClientAccess()]
    public class DomainService1 : LinqToEntitiesDomainService<TESTDBEntities>
    {
        public IQueryable<Lecture> GetLectures()
        {
            return this.ObjectContext.Lectures;
        }      
        public IQueryable<ProjectGroup> GetProjectGroups()
        {
            return this.ObjectContext.ProjectGroups;
        }
        public IQueryable<Student> GetStudents()
        {
            return this.ObjectContext.Students;
        }
    }

我需要向域服务添加一个方法,为给定的项目组ID返回List<Student>

List<Student> GetStudentsByProject(int pgid)

OR

IQueryable<Student> GetStudentsByProject(int pgid)

由于这涉及加入,我想我必须手动更改Model.edmx以及DomainService.cs文件。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

您不必更改.edmx文件。

public List<Student> GetStudentsByProject(int pgid)
{
    return this.ObjectContext.ProjectGroup.Where(pg => pg.pgid == pgid)
               .SelectMany(pg => pg.Students).ToList();
}

答案 1 :(得分:1)

只需在您的域名服务中添加其他方法即可:

public IQueryable<Student> GetStudentsByGroup(int projectGroupId)
{
    return this.ObjectContext.Students
               .Where(x => x.ProjectGroup.pgid == projectGroupId);
}