【发布时间】:2014-12-05 21:21:52
【问题描述】:
我正在尝试将 Infragistics UltraWinGrid 绑定到我的实体:
我的一个模型:
public class Student
{
public int ID { get; set; }
public String Name { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
DbContext 类:
public class AppContext: DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
}
通用存储库的实现
public class StudentRepository : Repository<Student>
{
public StudentRepository(AppContext context) : base(context) { }
public IEnumerable<Student> GetAllStudents()
{
return context.Students.Include("Course").ToList();
}
public void Save()
{
context.SaveChanges();
}
}
还有一个数据访问层:
public class DAL : IUnitOfWork
{
private AppContext dbContext;
private StudentRepository student;
public DAL()
{
dbContext = new AppContext();
}
// the following is probably ugly
public AppContext getContext()
{
return this.dbContext;
}
public StudentRepository Students
{
get
{
if (students== null)
students= new StudentRepository(dbContext);
return students;
}
}
public int Save()
{
return dbContext.SaveChanges();
}
public void Dispose()
{
if (students != null)
students.Dispose();
GC.SuppressFinalize(this);
}
}
到目前为止,我一直使用dal.Students.GetAllStudents().toList() 作为数据源,但这显然只适用于仅用于读取目的的网格。现在我想使用网格的更新、插入和删除功能,但我不知道如何使用。
我在互联网上找到的唯一方法是在上下文中访问学生DbSet:
grid.DataSource = dal.getContext().Students.Local.ToBindingList();
真的应该这样吗?这样一来,就显得相当丑陋了。
【问题讨论】:
标签: c# .net entity-framework datagrid infragistics