2016-02-05 37 views
0

我需要从使用实体框架创建的表中获取数据。现在,web api项目负责从我创建的测试数据中生成表格。 。从执行方法中的实体框架表中检索数据

的网络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的instans并调用简单的方法公共DbSet预订{获得;组; }

我该如何从表中获取数据?

回答

0

您应该在您的DAL中创建单独的项目,然后在WebApi项目和您的类库中添加对它的引用。

+0

好的..我会试试。 – Mandersen