【问题标题】:Entity Framework Code First creates classes from two tables and relationships one to manyEntity Framework Code First 从两个表和一对多关系创建类
【发布时间】:2019-07-11 07:27:34
【问题描述】:

我创建了一个应用程序,作为测试示例,我使用了一张订单表。我对类建模有疑问。

我有 3 个课程:

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

不知道这个型号好不好。你怎么看?因为这里没有东西:/在应用程序中:

  • 我无法将汽车或零件添加到数据库中没有的订单中。

  • 在订单表中,我只想查看订单 ID、订单价值、汽车 ID 和所购买零件的 ID。

     

我希望 Car 和 Part 表中没有关于订单的数据。我只想在应用程序中添加零件或汽车,以后只能在订单部分从中选择。

【问题讨论】:

  • 一个订单可以只包含一个汽车和单个零件,还是同一订单中包含多个汽车和零件?就目前而言,您订购的课程正在向 EF 提供相互矛盾的信息。
  • @LorenPaulsen 订单可能很少有汽车和零件。

标签: c# entity-framework ef-code-first code-first


【解决方案1】:

让我们从您需要的物理表开始:

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

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

最后两个表称为“连接表”,因为您需要它们能够存储同一订单中的多个零件和多辆汽车,但实际上并不是您认为是模型一部分的表。

如果您按如下方式设置类,Entity Framework 将自动生成这些连接表:

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

请注意,Car 和 Part 表上的 ICollection 属性将为 EF 提供创建连接表所需的线索。另外,请记住您的导航属性需要“虚拟”。

【讨论】:

    【解决方案2】:

    模型不错?

    • 一个比萨饼可能有一些不足
    • 一个比萨饼下可能有一种酱汁
    • 一个订单可能有一些 idgredience 和一些酱汁。

    这是我的课:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      相关资源
      最近更新 更多