1-Exam17-DAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Exam17.Model;
using InterFace;
namespace Exam17.DAL
{
public class CommonDal : interface1
{
Model1 db = new Model1();
public int Add(ProductInfo m)
{
//db.Database.CreateIfNotExists();
db.ProductInfos.Add(m);

return db.SaveChanges();
}

public int Del(int ID)
{
var model = db.ProductInfos.Find(ID);
db.ProductInfos.Remove(model);
return db.SaveChanges();

}
//批量删除
public int Dels(string IDs)
{
string[] s = IDs.Split(',');
List<ProductInfo> ss = new List<ProductInfo>();
foreach (var item in s)
{
var model = db.ProductInfos.Find(Convert.ToInt32(item));
ss.Add(model);
}
db.ProductInfos.RemoveRange(ss);
return db.SaveChanges();
}

public ProductInfo Fill(int ID)
{
return db.ProductInfos.Where(p => p.ID.Equals(ID)).FirstOrDefault();
}

public List<ProductInfo> Show(Expression<Func<ProductInfo, bool>> Where)
{
return db.ProductInfos.Where(Where).ToList();
}

public int UpdateState(int ID, int State)
{
var lst = db.ProductInfos.Where(t => t.ID.Equals(ID)).ToList();
if(lst.Count>0)
{
if(State==1)
{
lst[0].State = 0;
}
else
{
lst[0].State = 1;
}

db.Entry(lst[0]).State = System.Data.Entity.EntityState.Modified;
}

return db.SaveChanges();
}

//反填
public ProductInfo fan(int ID)
{
Model1 db = new Model1();
return db.ProductInfos.Where(p => p.ID.Equals(ID)).FirstOrDefault();
}

//修改I
public int xiu(ProductInfo model)
{
Model1 db = new Model1();

var list = db.ProductInfos.Where(p => p.ID == model.ID).FirstOrDefault();

list.ProductTitle = model.ProductTitle;
list.OrderBy = model.OrderBy;
list.Price = model.Price;
list.Files = model.Files;

list.Content = model.Content;
list.BeginTime = model.BeginTime;
list.State = model.State;

return db.SaveChanges();
}

public int Update(ProductInfo m)
{
throw new NotImplementedException();
}
}
}

猜你喜欢

转载自www.cnblogs.com/PingShengI/p/10151264.html
DAL