我试图在一个网上商店项目中创造购买的历史,我希望类历史有购物车中的产品,我从来没有做过多对一的关系(我认为是最适合的),你觉得呢?
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; }
}发布于 2017-06-19 05:51:48
命名约定!如果你想要一个历史有很多衣服,而一件衣服只有一个历史,那么下面的代码很好用,这是没有实际意义的:
[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; }
}https://stackoverflow.com/questions/44615914
复制相似问题