2017-06-18 11 views
0

ウェブショッププロジェクトで購入の履歴を作成しようとしていますが、クラス履歴にカートの商品を持たせたいと思います。一人(私は最も執行猶予を受けていると思います)、どう思いますか?MVC-5のリレーション1対多

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

あなたの名前は混乱しているようです。 1つの「歴史」(それは何ですか)には多くの「衣服」がありますか?複数の名前を持つ単一のクラスは奇妙に見えます... – 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; } 
} 
関連する問題