【问题标题】:Retrieve data from the entity framework table in my execute method在我的执行方法中从实体框架表中检索数据
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 好的..我会试试的。
    猜你喜欢
    • 1970-01-01
    • 2014-02-28
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多