简体   繁体   中英

Entity Framework Code First creates classes from two tables and relationships one to many

I create an application and as an example for testing I take a table of orders. I have questions about class modeling.

I have 3 classes:

public class Car
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}
public class Part
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}


class Order
{
    public Order()
    {
        Cars = new List<Car>();
        Parts = new List<Part>();
    }

    public int OrderId { get; set; }

    public int CarId { get; set; }
    public int PartId { get; set; }

    public ICollection<Car> Cars { get; set; }
    public ICollection<Part> Parts { get; set; }
}

I do not know if this model is ok. What do you think? Because something does not go here: / In the application:

  • I can not add cars or parts to the order that I do not have in the database.

  • In the table of orders I would like to see only the order Id, the value of the order, and the Id of the car and Id of the part that was bought.

I would like the Car and Part tables to have no data about orders. I would like to only add parts or cars in the application, later only be able to select from them in the order section.

Let's start with the physical tables you will need:

Part { Id, Name, Price }
Car { Id, Name, Price } 
Order { Id }

OrderPart* { OrderId, PartId }
OrderCar* { OrderId, CarId }

The last two tables are called "join tables" because you need them to be able to store multiple parts and multiple cars on the same order, but are not really tables you think of as being part of your model.

Entity Framework will automatically make these join tables if you set up your classes as follows:

public class Car
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}
public class Part
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}

class Order
{
    public Order()
    {
        Cars = new List<Car>();
        Parts = new List<Part>();
    }

    public int OrderId { get; set; }

    public virtual ICollection<Car> Cars { get; set; }
    public virtual ICollection<Part> Parts { get; set; }
}

Note that the ICollection<> properties on the Car and Part table will be the clue to EF that it needs to make the join table. Also, remember that you need "virtual" on your navigation properties.

It is good model ?

  • One Pizza may have a few idgredience
  • One Pizza may have one sauce under the cheese
  • One Order may have a few idgredience and a few sauces.

It is my classes :

    public class Suace
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}

public class Pizza
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
    public ICollection<Idgredient> Idgredients { get; set; }
    public Sauce Sauce {get;set;}

    public virtual ICollection<Order> Orders { get; set; }
}

class Order
{
    public Order()
    {
        Cars = new List<Car>();
        Parts = new List<Part>();
    }

    public int OrderId { get; set; }

    public virtual ICollection<Car> Suace  { get; set; }
    public virtual ICollection<Part> Pizza { get; set; }
}

public class Idgredient
{
    public int Id { get; set; }
    public string Name { get; set; }


    public virtual ICollection<Pizza> Pizzas { get; set; }
}

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