2017-06-18 67 views
0

我试图在网上商店项目中创造购买历史,我希望课程历史能够从购物车获得产品,我从来没有做过多关系-one(我认为这是最不适合的),你怎么看待它?MVC-5中的一对多关系

public class Clothes 
     { 
      [Key] 
      public int Id { get; set; } 


      public ClothesType Type { get; set; } 

      public int Amount { get; set; } 

      [Range(10, 150)] 
      public double Price { get; set; } 

      public string ImagePath { get; set; } 

      public virtual History historyID { get; set; } 
     } 

public class History 
    { 
     [Key] 
     [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
     public int historyID { get; set; } 
     public string Email { get; set; } 
     public string Address { get; set; } 
     public string City { get; set; } 
     public DateTime ShipDate { get; set; } 
     public int Price { get; set; } 
     public virtual ICollection<Clothes> HistClothes { get; set; } 
    } 
+0

你的命名看起来很混乱。一个“历史”(那是什么)有许多“信仰”?一个具有复数名字的班级似乎很奇怪...... – oerkelens

回答

0

命名约定!下面的代码顺利的话,如果你想有一个历史上有很多衣服,衣来只有一个历史至极并没有真正意义:

[Table("Clothes")] 
public class Clothe 
    { 
     [Key] 
     public int Id { get; set; } 
     public ClothesType Type { get; set; } 
     public int Amount { get; set; } 
     [Range(10, 150)] 
     public double Price { get; set; } 
     public string ImagePath { get; set; } 
     public History historyID { get; set; } 
    } 

public class History 
{ 
    [Key] 
    public int historyID { get; set; } 
    public string Email { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public DateTime ShipDate { get; set; } 
    public int Price { get; set; } 
    public virtual ICollection<Clothe> ClothesHistory { get; set; } 

    public History() 
    { 
     ClothesHistory = new List<Clothe>(); 
    } 
} 

此代码,而不是顺利的话,如果你想有一个很多历史对于同一件衣服和每件衣服单件衣服:

[Table("Clothes")] 
public class Clothe 
    { 
     [Key] 
     public int Id { get; set; } 
     public ClothesType Type { get; set; } 
     public int Amount { get; set; } 
     [Range(10, 150)] 
     public double Price { get; set; } 
     public string ImagePath { get; set; } 
     public ICollection<History> Histories { get; set; } 

     public History() 
     { 
      Histories = new List<History>(); 
     } 
    } 

public class History 
{ 
    [Key] 
    public int historyID { get; set; } 
    public string Email { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public DateTime ShipDate { get; set; } 
    public int Price { get; set; } 
    [Required] 
    public Clothe RelatedClothe { get; set; } 
}