--------BLL
GoodDal dal = new GoodDal();
/// <summary>
/// 添加
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int Add(Good model)
{
return dal.Add(model);
}
/// <summary>
/// 绑定分类
/// </summary>
/// <returns></returns>
public List<GoodType> BandType()
{
return dal.BandType();
}
/// <summary>
/// 绑定供应商
/// </summary>
/// <returns></returns>
public List<Supplier> BandSupplier()
{
return dal.BandSupplier();
}
/// <summary>
/// 显示,查询
/// </summary>
/// <returns></returns>
public List<Good> Show(string GoodName, int TypeId, int SupplierId)
{
return dal.Show(GoodName, TypeId, SupplierId);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int Delete(string id)
{
if (string.IsNullOrEmpty(id))
{
return 0;
}
else
{
return dal.Delete(id);
}
}
#region 修改
/// <summary>
/// 反填
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Good FanTian(int id)
{
return dal.FanTian(id);
}
/// <summary>
/// 修改
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int Update(Good model)
{
return dal.Update(model);
}
------------DAL
GoodContext db = new GoodContext();
#region 添加
/// <summary>
/// 添加
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int Add(Good model)
{
try
{
db.Good.Add(model);
return db.SaveChanges();
}
catch (Exception)
{
throw;
}
}
#endregion
#region 绑定下拉
/// <summary>
/// 绑定分类
/// </summary>
/// <returns></returns>
public List<GoodType> BandType()
{
return db.GoodType.ToList();
}
/// <summary>
/// 绑定供应商
/// </summary>
/// <returns></returns>
public List<Supplier> BandSupplier()
{
return db.Supplier.ToList();
}
#endregion
#region 显示
/// <summary>
/// 显示,查询
/// </summary>
/// <returns></returns>
public List<Good> Show(string GoodName, int TypeId, int SupplierId)
{
List<Good> list = db.Good.Include("GoodType").Include("Supplier").ToList();
if (!string.IsNullOrEmpty(GoodName))//根据商品名称查询
{
list = list.Where(s => s.GName.Contains(GoodName)).ToList();
}
if (TypeId != 0)//根据类别查询
{
list = list.Where(s => s.TypeId == TypeId).ToList();
}
if (SupplierId != 0)//根据供应商查询
{
list = list.Where(s => s.SupplierId == SupplierId).ToList();
}
return list;
}
#endregion
#region 删除
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int Delete(string id)
{
string[] str = id.Split(',');
for (int i = 0; i < str.Length; i++)
{
int gid = int.Parse(str[i]);
//找到要删除的数据
Good model = db.Good.Where(s => s.Id == gid).ToList().First();
//删除
db.Good.Remove(model);
}
return db.SaveChanges();
}
#endregion
#region 修改
/// <summary>
/// 反填
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Good FanTian(int id)
{
//找到要修改的数据
Good model = db.Good.Where(s => s.Id == id).ToList().First();
return model;
}
/// <summary>
/// 修改
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int Update(Good model)
{
try
{
db.Good.Attach(model);
db.Entry(model).State = EntityState.Modified;
return db.SaveChanges();
}
catch (Exception)
{
throw;
}
}
----------DAL里对应异常
public GoodContext() : base("Good") {
}
public DbSet<Good> Good { get; set; }
public DbSet<GoodType> GoodType { get; set; }
public DbSet<Supplier> Supplier { get; set; }