Отношение 1-ко-многим в MVC-5 с

Я пытаюсь сделать историю покупок в проекте интернет-магазина, я хочу, чтобы у класса History был продукт из корзины, я никогда не делал отношения много-к-одному (я думаю, что он наиболее приспособлен для оценки), что ты думаешь об этом?

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

1 ответ

Соглашения об именах! Следующий код хорошо работает, если вы хотите, чтобы в истории было много одежды, а в одежде - только одна история, которая на самом деле не имеет смысла:

[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; }
}
Другие вопросы по тегам