首先我们需要在Model里创建类库有几张表最好创建几个类库
根据sql表示,里面包含了两张表ProductCategory(产品类别表)和Product(产品表)
--产品类别表
create table ProductCategory
(
Id int primary key identity(1,1), --主键,标识列 主键Id
Name nvarchar(16) not null --不能为空 类别名称
)
--产品表
create table Product
(
Id int primary key identity(1,1), --主键,标识列 主键Id
ProductName nvarchar(32) not null, --不能为空 产品名称
MarketPrice decimal(16,2) not null, --不能为空 市场价
SellingPrice decimal(16,2) not null, --不能为空 售价
CategoryId int references ProductCategory(Id), --外键,不能为空 类别Id
Introduction nvarchar(128) not null, --不能为空 产品介绍
IsOnSale bit not null, --不能为空 是否上架
AddTime datetime default getdate() not null--添加时间
)
那么我们就在Model中创建ProductCategory(产品类别表)和Product(产品表)
并对该数据进行封装
Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
/// <summary>
/// 产品表
/// </summary>
public class Product
{
/// <summary>
/// 主键Id
/// </summary>
public int Id { get; set; }
/// <summary>
/// 产品名称
/// </summary>
public string ProductName { get; set; }
/// <summary>
/// 市场价
/// </summary>
public double MarketPrice { get; set; }
/// <summary>
/// 售价
/// </summary>
public double SellingPrice { get; set; }
/// <summary>
/// 类别Id
/// </summary>
public int CategoryId { get; set; }
/// <summary>
/// 产品介绍
/// </summary>
public string Introduction { get; set; }
/// <summary>
/// 是否上架
/// </summary>
public int IsOnSale { get; set; }
/// <summary>
/// 添加时间
/// </summary>
public DateTime AddTime { get; set; }
}
}
ProductCategory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
/// <summary>
/// 产品类别表
/// </summary>
public class ProductCategory
{
/// <summary>
/// 主键Id
/// </summary>
public int Id { get; set; }
/// <summary>
/// 类别名称
/// </summary>
public string Name { get; set; }
}
}
特别注意:1、新建的类库中不会进行引用需手动添加public
2、封装的类型一定不能搞错,如时间用datetime