Add
1
2 /// <summary>
3 ///
4 /// </summary>
5 public void Add()
6 {
7 TestDBEntities2 testdb = new TestDBEntities2();
8
9 Test1 test1 = new Test1() { Name="yang"};
10
11 testdb.Tests.Add(test1);
12
13 testdb.SaveChanges();
14 }
Modify
1 public void Modify()
2 {
3 //-------方法1--------
4 // //1.先查询出来
5 Test1 test1 = testdb.Tests.Where(u => u.Name == "yang").FirstOrDefault();
6 ////2修改内容
7 test1.Name = "yang123";
8 ////3 提交
9 testdb.SaveChanges();
10 //4. 提交
11 testdb.SaveChanges();
12 }
13
14
15 public void Modify2()
16 {
17 //1.new对象
18 Test1 test1 = new Test1() { Id = 11, Name = "yang1" };
19
20 #region 修改部分字段
21 //2.附加对象
22 testdb.Tests.Attach(test1);
23 //3.修改部分字段
24 testdb.Entry(test1).Property("Name").IsModified = true;
25 #endregion
26
27 #region 修改全部字段
28 //2.修改全部字段
29 testdb.Entry(test1).State = System.Data.EntityState.Modified;
30 #endregion
31
32 //4. 提交
33 testdb.SaveChanges();
34 }
Delete
1 public void Delete()
2 {
3 //1.先查
4 Test1 test1 = testdb.Tests.Where(u => u.Name == "yang").FirstOrDefault();
5 //2.再删除
6 testdb.Tests.Remove(test1);
7 //3.提交
8 testdb.SaveChanges();
9 }
10
11
12 public void Delete2()
13 {
14
15 //1. new对象
16 Test1 test1 = new Test1() { Name = "yang" };
17 //2.附加对象
18 testdb.Tests.Attach(test1);
19 //3.删除对象
20 testdb.Tests.Remove(test1);
21 //4. 提交
22 testdb.SaveChanges();
23 }
Select
1 public void Select()
2 {
3 List<Test1> list;
4 List<Test1> list1;
5
6
7 //开始没有查询 ToList后才开始查询
8 list = testdb.Tests.Where(u => u.Name == "yang").ToList();
9 list1 = (from u in testdb.Tests where u.Name == "yang" select u).ToList();
10
11 }