简体   繁体   中英

Can I create this configuration in Entity Framework?

So. I create web-app with ASP.NET MVC wich helps

How could I create this databases in Entity Framework or should I change the concept?

TABLE order //Table for orders
Id int,
Person nvarchar(50),

TABLE meal //List of meals
Id int
Description nvarchar(50)

TABLE meal-order //Many-to-one  table with count of meals
IdOrder int,
IdMeal int,
Count tinyint

The idea is: I wish order two Pepperoni and Coke and my order will be saved as:

TABLE order
orderId, My Name 

TABLE meal (it has some stuff)
pepperoniId, Pepperoni
cokeId, Coke
hotDogId, Hot Dog

TABLE meal-order
orderId, pepperoniId, 2 (and here 2 is count of pepperoni I ordered)
orderId, cokeId, 1 (just one coke)

Is it possible to realise meal-order table with counter? If it is very hard, could you propose another concept of tables for my database?

That's pretty much a classical Order - OrderDetail - Product model where your Meal is a Product and the MealOrder is an OrderDetail . I would prefer to call the MealOrder entity/table OrderDetail because the fact that it has a reference to a Meal is perhaps only one of many attributes. For example, the OrderDetail might also have a Discount property that is not directly related to the meals but to the time when the order has been placed ("30 percent discount for all drinks on Mondays!").

The entity model might look like this:

public class Order
{
    public int Id { get; set; }

    public string PersonName { get; set; } // the "Customer"
    // ... more properties

    // an order has many OrderDetails/meals
    public virtual ICollection<OrderDetail> Details { get; set; }
}

public class OrderDetail // your "order-meal"
{
    public int Id { get; set; }

    public int OrderId { get; set; }
    public virtual Order Order { get; set; }

    public int MealId { get; set; }
    public virtual Meal Meal { get; set; }

    public int Quantity { get; set; } // your Count

    // ...more properties like for example decimal Discount
}

public class Meal // the "Product"
{
    public int Id { get; set; }

    public string Description { get; set; }
    // ...more properties
}

The database tables would be like sketched in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM