EF增删改查

//添加

using (WanWeiTiKuEntities WanweiTiku = new WanWeiTiKuEntities())
{
tk_ShiJuanLeiXin Leixing = new tk_ShiJuanLeiXin()
{
LeiXing_Name = TxtLeixing.Text.Trim(),
KeCheng_id = Frmain.SelectKechengId
};
WanweiTiku.tk_ShiJuanLeiXin.Add(Leixing);
WanweiTiku.SaveChanges();
}

//删除

WanWeiTiKuEntities WanweiTiku = new WanWeiTiKuEntities();
var rmZhishidian = (from u in WanweiTiku.tk_ZhiShiDian
where u.ZhiShiDian_id == 1
select u).ToList();
foreach (var item in rmZhishidian)
{
WanweiTiku.tk_ZhiShiDian.Remove(item);
}
WanweiTiku.SaveChanges();

 

//修改

WanWeiTiKuEntities WanweiTiku = new WanWeiTiKuEntities();

var result = from u in WanweiTiku.tk_Student

 select u;

////使用循环方式,修改所有查询到的记录
foreach (var item in result)
{
 item.Age = 10;
}

WanweiTiku .SaveChanges();

//查询

//使用匿名类查询多个字段

WanWeiTiKuEntities WanweiTiku = new WanWeiTiKuEntities();

var result = from p in WanweiTiku.Student
                  where p.ID < 4
                  select new { p.ID, p.Name };

foreach (var item in result)
{
////这里无法输出年龄
Console.WriteLine("ID:{0},姓名:{1}。", item.ID, item.Name);
}

posted on 2014-03-20 19:45  菜鸟阿文  阅读(183)  评论(0编辑  收藏  举报