1.
ALinq 之DataContext簡介
本系列文章使用SQL Server2005 資料庫,.NET 3.5 環境。
首先我們建立一個OA_DB資料庫,其中建立了三張表,表結構如下圖:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnauUTY5UGZwEDZxYjM1gDZ0M2NlRDO0QDZlhzM4EjYzkjYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.jpg)
ORM 映射的主要作用使用對象形式操作資料庫,那我們如何使用對象形式去操作以上資料庫的三張表呢,那這裡講到的的就是DataContext。DataContext 的作用很明确,就和Linq to SQL 中的DataContext 的作用一樣:
下面看看OA_DB 的DataContext源碼檔案:
ALinq DataContext
1 [ALinq.Mapping.DatabaseAttribute(Name="OA_DB")]
2 [ALinq.Mapping.ProviderAttribute(typeof(ALinq.SqlClient.Sql2000Provider))]
3 public partial class OA_DBDataContext : ALinq.DataContext
4 {
5
6 private static ALinq.Mapping.MappingSource mappingSource = new ALinq.Mapping.AttributeMappingSource();
7
8 #region Extensibility Method Definitions
9 partial void OnCreated();
10 partial void InsertTabMenu(TabMenu instance);
11 partial void UpdateTabMenu(TabMenu instance);
12 partial void DeleteTabMenu(TabMenu instance);
13 partial void InsertTabUser(TabUser instance);
14 partial void UpdateTabUser(TabUser instance);
15 partial void DeleteTabUser(TabUser instance);
16 partial void InsertTabRole(TabRole instance);
17 partial void UpdateTabRole(TabRole instance);
18 partial void DeleteTabRole(TabRole instance);
19 #endregion
20
21 public OA_DBDataContext() :
22 base("Data Source=C23471744F6D4BC\\SQLEXPRESS;Initial Catalog=OA_DB;Integrated Security=" +
23 "True", mappingSource)
24 {
25 OnCreated();
26 }
27
28 public OA_DBDataContext(string connection) :
29 base(connection, mappingSource)
30 {
31 OnCreated();
32 }
33
34 public OA_DBDataContext(System.Data.IDbConnection connection) :
35 base(connection, mappingSource)
36 {
37 OnCreated();
38 }
39
40 public OA_DBDataContext(string connection, ALinq.Mapping.MappingSource mappingSource) :
41 base(connection, mappingSource)
42 {
43 OnCreated();
44 }
45
46 public OA_DBDataContext(System.Data.IDbConnection connection, ALinq.Mapping.MappingSource mappingSource) :
47 base(connection, mappingSource)
48 {
49 OnCreated();
50 }
51
52 public ALinq.Table<TabMenu> TabMenu
53 {
54 get
55 {
56 return this.GetTable<TabMenu>();
57 }
58 }
59
60 public ALinq.Table<TabUser> TabUser
61 {
62 get
63 {
64 return this.GetTable<TabUser>();
65 }
66 }
67
68 public ALinq.Table<TabRole> TabRole
69 {
70 get
71 {
72 return this.GetTable<TabRole>();
73 }
74 }
75 }
OA_DBDataContext 繼承ALinq.DataContext,它和System.Data.Linq.DataContext
的作用大同小異,隻是命名空間的不同。
ALinq 中的DataContext使用了兩個特性來修飾,其中和Linq to SQL 名字相同的特性源碼大緻如下:
DataAttribute
1 public abstract class DataAttribute : Attribute
2 {
3 // Methods
4 protected DataAttribute();
5
6 // Properties
7 public string Name { get; set; }
8 public string Storage { get; set; }
9
10 // Fields
11 private string a;
12 private string b;
13 }
14
15
上面的代碼是通過反編譯工具編譯出來的,可能與源碼有些出入,但是不影響閱讀
前面說到過ALinq 的最大特點就是能夠相容多種資料庫,而特性ProviderAttribute 就是對于相容多種資料庫起着關鍵性的作用。
ProviderAttribute
1 [AttributeUsage(AttributeTargets.Class)]
2 public sealed class ProviderAttribute : Attribute
3 {
4 // Methods
5 public ProviderAttribute();
6 public ProviderAttribute(Type type);
7
8 // Properties
9 public Type Type { get; }
10
11 // Fields
12 private Type a;
2.
ALinq 之DataContext使用
DataContext 檢視資料庫的基本屬性:
測試代碼:
DataContext 屬性測試代碼
1 static void Main(string[] args)
2 {
3 string connectionString = @"server=C23471744F6D4BC\SQLEXPRESS;database=OA_DB;Integrated Security=true";
4 OA_DBDataContext context = new OA_DBDataContext(new SqlConnection(connectionString));
5 Console.WriteLine("資料庫是否存在:" + context.DatabaseExists());
6 Console.WriteLine("請求逾時時間:" + context.CommandTimeout);
7 Console.WriteLine("資料庫連接配接字元串:" + context.Connection.ConnectionString);
8 Console.WriteLine("資料庫連接配接逾時時間:" + context.Connection.ConnectionTimeout);
9 Console.WriteLine("資料庫名稱:" + context.Mapping.DatabaseName);
10
11 }
測試結果:
3.
ALinq 之DataContext 日志
Linq to SQL 有日志功能,其實ALInq 也有日志功能:日志分為兩種情況:
ALinq 日志功能測試
11 StreamWriter sw = new StreamWriter("log.txt", true);
12 context.Log = Console.Out;
13 var linq = from c in context.TabMenu select c;
14 context.Log.Write(linq.ToString());
15 sw.Close();
16 }
我們看到 context.Log = Console.Out; 這個裡面在控制台輸出了linq 查詢的sql語句。
日志測試結果
還有一種情況是context.Log = sw; 使用這種情況,是将日志輸出到指定的日志檔案中。