0

我正在尝试首先使用代码为 asp.net mvc 应用程序生成数据库。OrderItem 类确实生成为数据库中的 OrderItems 表,但我最终无法访问它。我该怎么做才能允许以下例如:db.OrderItems.Find(id);

模型如下:

namespace CustomerOrders.Models{

public class Customer
{

    public virtual int  CustomerID { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Company { get; set; }
    public virtual string Email { get; set; }
    public virtual string EmailCheck { get; set; }
}

public class Order
{
    public virtual int OrderID { get; set; }
    public virtual int CustomerID { get; set; }
    public virtual DateTime OrderDate { get; set; }
    public virtual double OrderTotal { get; set; }
    public virtual double Tax { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual List<OrderItem> OrderItems { get; set; }
}

public class OrderItem
{
    public virtual int OrderItemID { get; set; }
    public virtual int OrderID { get; set; }
    public virtual int ProductID { get; set; }
    public virtual double PricePerItem { get; set; }
    public virtual double Quantity { get; set; }
    public virtual Product Product { get; set; }
    public Order Order { get; set; }
}

public class Product
{
    public virtual int ProductID { get; set; }
    public virtual string Title { get; set; }
    public virtual string Description { get; set; }
    public virtual double Price { get; set; }
}
}
4

1 回答 1

0

您可能在 dbContext 中没有声明,因此打开 dbContext 并添加以下内容: public DbSet<OrderItem> OrderItems { get; set; }

你应该得到这个:

public class CustomerOrdersDB : DbContext
{
    public DbSet<Order> Orders { get; set; }

    public DbSet<Customer> Customers { get; set; }

    public DbSet<Product> Products { get; set; }

    public DbSet<OrderItem> OrderItems { get; set; }
}
于 2012-05-27T09:40:23.367 回答