一.实体类
1.附默认值时要在实体类里操作
例
2.外键约束要添加导航属性,不然数据牵引不会成功
例
二.数据迁移
1.匿名对象添加数据
protected override void Seed(Day0716ZuoYe.DAL.ZhaoDB context)
{
context.TypeInfo.AddOrUpdate(new Models.TypeInfo()
{
TName="运营类"
});
}
三.DAL层代码
1.添加:
public int Add(ZhaoPinInfo m)
{
try
{
db.ZhaoPinInfo.Add(m);
var flag = db.SaveChanges();
return flag;
}
catch (Exception)
{
throw;
}
}
2.分页查询
public List<ZhaoPinInfo> PageShow(out int totalpage,out int totalcount,int? tid,int? sid ,int pageindex,int pagesize)
{
IQueryable<ZhaoPinInfo> query = db.ZhaoPinInfo.Include("TypeInfo").Include("sectorInfo");
if(tid!=null)
{
query = query.Where(u => u.TypeId == tid);
}
if (sid != null)
{
query = query.Where(u => u.stId == sid);
}
totalcount = query.Count();
totalpage = Convert.ToInt32(Math.Ceiling(totalcount * 1.0 / pagesize));
//分页之前必须排序 Skip 跳过多少条 Take 显示多少条
return query.OrderBy(m => m.Zid).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
}
3.批量删除
public int PiDel(string ids)
{
var idss = ids.Split(',');
foreach (var item in idss)
{
db.ZhaoPinInfo.Remove(db.ZhaoPinInfo.Find(Convert.ToInt32(item)));
}
return db.SaveChanges();
}
4.反填:
public ZhaoPinInfo GetZhao(int id)
{
return db.ZhaoPinInfo.Find(id);
}
5.修改
public int Update(ZhaoPinInfo m)
{
db.Entry(m).State = System.Data.Entity.EntityState.Modified;
return db.SaveChanges();
}