1.首先先创建一个数据模型类Product;
namespace MVCGW.Models
{
public class Product
{
public int ID { get; set; }
[Display(Name = "商品名称")]
[Required(ErrorMessage = "必填")]
[StringLength(60, MinimumLength = 3, ErrorMessage = "必须是[3,60]个字符")]
public string Title { get; set; }
[Display(Name = "商品类型")]
[Required]
public string Genre { get; set; }
[Display(Name = "商品简介")]
[Required]
public string Description { get; set; }
[Display(Name = "商品售价")]
[Range(1, 10000)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
[Display(Name = "上新日期")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Display(Name = "商品图片")]
[Required]
public string Photo { get; set; }
}
还有ShoppingCar(购物车)类和Order(订单)类,
为了在数据库中保存图片,保存的是图片路径;
2.创建数据库上下文类;
public class ProductDBContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ShoppingCar> Shoppingcars { get; set; }
public DbSet<Order> Orders { get; set; }
}
3.创建数据库连接串;
Web.config文件: <connectionStrings>
<add name="ProductDBContext"