Entity Framework学习 - 2.增删改查

1.增加数据

PirateBayEntities db = new PirateBayEntities();
T_Tests test = new T_Tests();
test.Name = "老李";
test.Age = 20;
db.T_Tests.Add(test);
db.SaveChanges();

 

2.删除数据

PirateBayEntities db = new PirateBayEntities();
T_Tests test = new T_Tests() {ID = 1};
db.T_Tests.Attach(test);
db.T_Tests.Remove(test);
db.SaveChanges();

注意,此处只能是主键ID

 

3.修改数据

PirateBayEntities db = new PirateBayEntities();
table_test tt = db.table_test.FirstOrDefault(x => x.name == "abc");
tt.age = 88;
db.table_test.Attach(tt);
db.Entry(tt).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

 

4.查询数据

TM173Entities db = new TM173Entities();
var list = db.T_BillSource.Where(a => a.SourceID == 0).ToList();

可以获取一堆,也可以

TM173Entities db = new TM173Entities();
var list = db.T_BillSource.Where(a => a.SourceID == 0).FirstOrDefault();

获取一个

posted @ 2015-05-24 22:25  朋克  阅读(333)  评论(0编辑  收藏  举报