using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EFLearnFirst
{
class Program
{
static void Main(string[] args)
{
try
{
//HouseEntities house = new HouseEntities();
//T_Desk desk = new T_Desk();
//desk.Adress = "北京市";
//desk.Name = "wang";
//desk.Phone = "12838839";
//house.T_Desk.AddObject(desk);
//house.SaveChanges();
T_Desk desk = findDeskById(2);
desk.Name = "wangyonghua";
Update(desk);
T_Desk desk1 = findDeskById(1);
delete(desk1);
}
catch (Exception err)
{
throw new Exception("错误:"+err.Message);
}
}
/// <summary>
/// 插入语句
/// </summary>
public static void Insert(T_Desk desk)
{
HouseEntities houseEntites = new HouseEntities();
houseEntites.T_Desk.AddObject(desk);
houseEntites.SaveChanges();
}
/// <summary>
/// 更新
/// </summary>
public static void Update(T_Desk desk)
{
HouseEntities houseEntites=new HouseEntities ();
IQueryable<T_Desk> desks = from d in houseEntites.T_Desk where d.Id == desk.Id select d;
T_Desk oldDesk = desks.SingleOrDefault();
oldDesk.Name = desk.Name;
oldDesk.Phone = desk.Phone;
houseEntites.SaveChanges();
}
/// <summary>
/// 删除
/// </summary>
public static void delete(T_Desk desk)
{
HouseEntities houseEntities = new HouseEntities();
houseEntities.T_Desk.DeleteObject(desk);
houseEntities.SaveChanges();
}
/// <summary>
/// 查询
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static T_Desk findDeskById(int id)
{
HouseEntities houseEntity=new HouseEntities();
IQueryable<T_Desk> desks = from d in houseEntity.T_Desk where d.Id == id select d;
return desks.SingleOrDefault();
}
}
}
转载于:https://www.cnblogs.com/wangyhua/archive/2012/09/06/4050635.html