【发布时间】:2016-05-15 12:06:39
【问题描述】:
我需要从使用实体框架创建的表中获取数据。现在,web api 项目负责从我创建的测试数据中生成表格。
web api 项目有一个名为 DAL 的文件夹,其中包含一个 BookingsContext 类。看起来像这样:
public class BookingsContext : DbContext
{
public BookingsContext() : base("BookingsContext")
{
}
// DbSet to bookings
public DbSet<Booking> Bookings { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
如果我在控制器中创建此类的一个实例,我可以调用 DbSet 方法从数据库中获取预订。
问题是我创建了一个名为 GetBookings 的类库。它负责从数据库中获取所有预订。
应该从数据库表中获取数据的类是我的 GetAllBookingsQuery。
// Generic interface
public interface IQuery<out T>
{
T Execute();
}
class GetAllBookingsQuery : IQuery<List<Booking>>
{
// Get a list of bookings from the database.
public List<Booking> Execute()
{
return null;
}
}
这里我不能创建 DbContext 的实例并调用简单的方法 public DbSet Bookings { get;放; }
我应该如何从表中获取数据?
【问题讨论】:
标签: c# entity-framework