2017-06-18 156 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; } 
}