1 数据连接并实例LightSpeedContext对象
2 public static readonly LightSpeedContext<LinghtSpeedModelUnitOfWork> context =
3 new LightSpeedContext<LinghtSpeedModelUnitOfWork>()
4 {
5 //连接字符串
6 ConnectionString = ConfigurationManager.ConnectionStrings["RetailSqlServer2005"].ToString(),
7 //使用的数据库
8 DataProvider = DataProvider.SqlServer2005,
9 //用KeyTable生成标识列
10 IdentityMethod = IdentityMethod.KeyTable,
11 //日志
12 Logger = new TraceLogger(),
13 //获取或设置一个值,该值指示是否表名称被推断为复数形式
14 PluralizeTableNames = false
15 };
16 public static readonly PerRequestUnitOfWorkScope<LinghtSpeedModelUnitOfWork> scop =
17 new PerRequestUnitOfWorkScope<LinghtSpeedModelUnitOfWork>(context);
18
19
20 简单增,册,改,查
21
22 //创建UnitOfWork对象
23 using (var con = Global.context .CreateUnitOfWork())
24 {
25 //添加
26 //Product2 tes = new Product2();
27 //tes.ProductName = "1656565";
28 //con.Add(tes);
29 //con.SaveChanges();
30
31 //根據相應的條件查詢出數據
32 //var pro = con.Find<Product2>(Entity.Attribute("Id") == 73);
33
34 //簡單的單行更新
35 //var pro = con.FindById<Product2>(73);
36 //pro.ProductName = "jeje";
37 //con.SaveChanges();
38 //Response.Write(pro.ProductName+pro.Id);
39
40 //在Productname中找到以寶開頭的所有信息
41 //var pro = new Query(typeof(Product2), Entity.Attribute("ProductName").Like("寶%"));
42 //更新的字段及要更換的值
43 //IDictionary<object, object> product = new Dictionary<object, object> { { "ProductName", "寶馬520" } };
44 //con.Update(pro, product);
45 //con.SaveChanges();
46 this.TextBox1.Text = con.Product2s.Count().ToString();
47
48 //通過實體Entity刪除
49 //var pro = con.FindById<Product2>(21);
50 //con.Remove(pro);
51 //con.SaveChanges();
52 //this.TextBox1.Text += con.Product2s.Count().ToString();
53
54 //通過LightSpeed Query對象刪除數據
55 //var prod = new Query(typeof(Product2)) { Identifier=31};
56 //con.Remove(prod);
57 //con.SaveChanges();
58 //this.TextBox1.Text += con.Product2s.Count().ToString();
59 var pro = con.Product2s.ToList<Product2>();