0
votes

I need to get data from a table I have created using the entity framework. Right now, the web api project is responsible to generate the table out of test data I've created.

The web api project has a folder called DAL with a BookingsContext class in. Looks like this:

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>();
    }
}

If I create an instant of this class in the controller, I can call the DbSet method to get the bookings from the database.

The problem is that I have created a class library called GetBookings. Its is responsible to get all the bookings from the database.

The class which should get the data from the database table is my 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;
    }
}

Here I can't make a instans of the DbContext and call the simple method public DbSet Bookings { get; set; }

How should I get the data from the table?

1

1 Answers

0
votes

You should create separate project with your DAL then add reference to it in both WebApi project and your class library.