我试图在网上商店项目中创建购买历史,我希望课程历史从购物车中获得产品,我从未做过多对一的关系(我认为是最适合的激动),您如何看待它?
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 :(得分: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; }
}