您的位置:首页 > 其它

综合应用WPF/WCF/WF/LINQ之十二:LINQ的ORM功能中对自定义的Entity Class的支持

2008-01-10 21:10 423 查看
首先,我们使用SqlMetal.exe程序从DBML文件生成Mapping程序:

1 SqlMetal.exe /code:"..\Eallies.OA.DAL.DBML\Database.cs" "..\Eallies.OA.DAL.DBML\Database.dbml"  我们查看生成的Database.cs文件,发现关于存储过程GetEmployeeByEmployeeId的代码如下:

1 [Function(Name="dbo.GetEmployeeByEmployeeId")]
2 public ISingleResult<GetEmployeeByEmployeeIdResult> GetEmployeeByEmployeeId([Parameter(Name="EmployeeId", DbType="Int")] System.Nullable<int> employeeId)
3 {
4 IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), employeeId);
5 return ((ISingleResult<GetEmployeeByEmployeeIdResult>)(result.ReturnValue));
6 }  显然,LINQ认为该存储过程返回的结果的Entity Class为GetEmployeeByEmployeeIdResult,但这却不是我们所期望的,实际上我们已经定义了Info类用于保存返回的结果:

1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Runtime.Serialization;
7 using System.Data.Linq;
8 using System.Data.Linq.Mapping;
9 using Eallies.OA.Info.Enum;
10
11 namespace Eallies.OA.Info
12 {
13 [DataContract]
14 [Serializable]
15 public class EmployeeInfo
16 {
17 private Int32 _EmployeeId;
18 private String _EmployeeNo;
19 private String _EmployeeName;
20 private Int32 _DepartmentId;
21 private Int32 _PositionId;
22 private Nullable<Int32> _EmployeeManager;
23 private Boolean _EmployeeGender;
24 private DateTime _EmployeeEntryDate;
25 private Nullable<DateTime> _EmoplyeeBirthday;
26 private String _EmployeePhone;
27 private String _EmployeeEmail;
28 private EmployeeStatusEnum _EmployeeStatus;
29
30 [DataMember]
31 [Column(Storage = "_EmployeeId", DbType = "Int NOT NULL", CanBeNull = false)]
32 public Int32 EmployeeId
33 {
34 get { return this._EmployeeId; }
35 set { this._EmployeeId = value; }
36 }
37
38 [DataMember]
39 [Column(Storage = "_EmployeeNo", DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
40 public String EmployeeNo
41 {
42 get { return this._EmployeeNo; }
43 set { this._EmployeeNo = value; }
44 }
45
46 [DataMember]
47 [Column(Storage = "_EmployeeName", DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
48 public String EmployeeName
49 {
50 get { return this._EmployeeName; }
51 set { this._EmployeeName = value; }
52 }
53
54 [DataMember]
55 [Column(Storage = "_DepartmentId", DbType = "Int NOT NULL", CanBeNull = false)]
56 public Int32 DepartmentId
57 {
58 get { return this._DepartmentId; }
59 set { this._DepartmentId = value; }
60 }
61
62 [DataMember]
63 [Column(Storage = "_PositionId", DbType = "Int NOT NULL", CanBeNull = false)]
64 public Int32 PositionId
65 {
66 get { return this._PositionId; }
67 set { this._PositionId = value; }
68 }
69
70 [DataMember]
71 [Column(Storage = "_EmployeeManager", DbType = "Int", CanBeNull = true)]
72 public Nullable<Int32> EmployeeManager
73 {
74 get { return this._EmployeeManager; }
75 set { this._EmployeeManager = value; }
76 }
77
78 [DataMember]
79 [Column(Storage = "_EmployeeGender", DbType = "Bit NOT NULL", CanBeNull = false)]
80 public Boolean EmployeeGender
81 {
82 get { return this._EmployeeGender; }
83 set { this._EmployeeGender = value; }
84 }
85
86 [DataMember]
87 [Column(Storage = "_EmployeeEntryDate", DbType = "DateTime NOT NULL", CanBeNull = false)]
88 public DateTime EmployeeEntryDate
89 {
90 get { return this._EmployeeEntryDate; }
91 set { this._EmployeeEntryDate = value; }
92 }
93
94 [DataMember]
95 [Column(Storage = "_EmoplyeeBirthday", DbType = "DateTime", CanBeNull = true)]
96 public Nullable<DateTime> EmoplyeeBirthday
97 {
98 get { return this._EmoplyeeBirthday; }
99 set { this._EmoplyeeBirthday = value; }
100 }
101
102 [DataMember]
103 [Column(Storage = "_EmployeePhone", DbType = "NVarChar(50)", CanBeNull = true)]
104 public String EmployeePhone
105 {
106 get { return this._EmployeePhone; }
107 set { this._EmployeePhone = value; }
108 }
109
110 [DataMember]
111 [Column(Storage = "_EmployeeEmail", DbType = "NVarChar(50)", CanBeNull = true)]
112 public String EmployeeEmail
113 {
114 get { return this._EmployeeEmail; }
115 set { this._EmployeeEmail = value; }
116 }
117
118 [DataMember]
119 [Column(Storage = "_EmployeeStatus", DbType = "Int NOT NULL", CanBeNull = false)]
120 public EmployeeStatusEnum EmployeeStatus
121 {
122 get { return this._EmployeeStatus; }
123 set { this._EmployeeStatus = value; }
124 }
125 }
126 }  这时,我们也可以手动或者采用程序将GetEmployeeByEmployeeIdResult改成EmployeeInfo,幸运的是,LINQ也能正确的识别我们的这种更改。然后,我们就可以将LINQ自动生成的GetEmployeeByEmployeeIdResult类彻底删除了。
  值得注意的是:由于Info类是应用于整个系统各个环节的,所以它有一些特别的定义。
  我们注意到这个类加了DataContract之类的标志,这是用于WCF技术的;它还加了Serializable标志,这是用于WF技术的,在将Workflow实例持久化到数据库中时,要求这个类可序列化;另外,它还加了Column标志,这是用于LINQ技术的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐