一直以来都在学习中,所以也没有什么好文章献给大家,这次小弟也是来提问的,Dudu觉得不合适可以从首页撤掉。不过本人认为让更多的人看到问题也是不错的。
问题如下:
建立了Book数据库,库中有三张表Orders,Products,Remarks。其中Orders的字段Oid是Products字段Poid的主键,意思是这两张表组成了主外键关系。Remarks是一张独立的表。利用Linq To SQL设计器生成了三张表的实体类。并将三个实体类暴露给客户端([DataContract]).代码如下:
Orders类:

Code
1
[Table(Name="dbo.orders")]
2
[DataContract(Name = "Orders", Namespace = "http://www.lgx.OrderLinq")]
3
public partial class order : INotifyPropertyChanging, INotifyPropertyChanged
4
{
5
6
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
7
8
private int _oid;
9
10
private System.Nullable<decimal> _totalPrice;
11
12
private System.DateTime _odate;
13
14
private EntitySet<product> _products;
15
16
Extensibility Method Definitions#region Extensibility Method Definitions
17
partial void OnLoaded();
18
partial void OnValidate(System.Data.Linq.ChangeAction action);
19
partial void OnCreated();
20
partial void OnoidChanging(int value);
21
partial void OnoidChanged();
22
partial void OntotalPriceChanging(System.Nullable<decimal> value);
23
partial void OntotalPriceChanged();
24
partial void OnodateChanging(System.DateTime value);
25
partial void OnodateChanged();
26
#endregion
27
28
public order()
29
{
30
this._products = new EntitySet<product>(new Action<product>(this.attach_products), new Action<product>(this.detach_products));
31
OnCreated();
32
}
33
34
[Column(Storage="_oid", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
35
[DataMember(Order=0)]
36
public int oid
37
{
38
get
39
{
40
return this._oid;
41
}
42
set
43
{
44
if ((this._oid != value))
45
{
46
this.OnoidChanging(value);
47
this.SendPropertyChanging();
48
this._oid = value;
49
this.SendPropertyChanged("oid");
50
this.OnoidChanged();
51
}
52
}
53
}
54
55
[Column(Storage="_totalPrice", DbType="Decimal(18,3)")]
56
[DataMember(Order = 1)]
57
public System.Nullable<decimal> totalPrice
58
{
59
get
60
{
61
return this._totalPrice;
62
}
63
set
64
{
65
if ((this._totalPrice != value))
66
{
67
this.OntotalPriceChanging(value);
68
this.SendPropertyChanging();
69
this._totalPrice = value;
70
this.SendPropertyChanged("totalPrice");
71
this.OntotalPriceChanged();
72
}
73
}
74
}
75
76
[Column(Storage="_odate", DbType="DateTime NOT NULL")]
77
[DataMember(Order = 2)]
78
public System.DateTime odate
79
{
80
get
81
{
82
return this._odate;
83
}
84
set
85
{
86
if ((this._odate != value))
87
{
88
this.OnodateChanging(value);
89
this.SendPropertyChanging();
90
this._odate = value;
91
this.SendPropertyChanged("odate");
92
this.OnodateChanged();
93
}
94
}
95
}
96
97
[Association(Name="order_product", Storage="_products", OtherKey="poid")]
98
[DataMember(Order = 3)]
99
public EntitySet<product> products
100
{
101
get
102
{
103
return this._products;
104
}
105
set
106
{
107
this._products.Assign(value);
108
}
109
}
110
111
public event PropertyChangingEventHandler PropertyChanging;
112
113
public event PropertyChangedEventHandler PropertyChanged;
114
115
protected virtual void SendPropertyChanging()
116
{
117
if ((this.PropertyChanging != null))
118
{
119
this.PropertyChanging(this, emptyChangingEventArgs);
120
}
121
}
122
123
protected virtual void SendPropertyChanged(String propertyName)
124
{
125
if ((this.PropertyChanged != null))
126
{
127
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
128
}
129
}
130
131
private void attach_products(product entity)
132
{
133
this.SendPropertyChanging();
134
entity.order = this;
135
}
136
137
private void detach_products(product entity)
138
{
139
this.SendPropertyChanging();
140
entity.order = null;
141
}
142
}
Products类:

Code
1
[Table(Name="dbo.products")]
2
[DataContract(Name = "Products", Namespace = "http://www.lgx.OrderLinq")]
3
public partial class product : INotifyPropertyChanging, INotifyPropertyChanged
4
{
5
6
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
7
8
private int _pid;
9
10
private int _poid;
11
12
private string _pName;
13
14
private decimal _price;
15
16
private System.DateTime _pdate;
17
18
private EntityRef<order> _order;
19
20
Extensibility Method Definitions#region Extensibility Method Definitions
21
partial void OnLoaded();
22
partial void OnValidate(System.Data.Linq.ChangeAction action);
23
partial void OnCreated();
24
partial void OnpidChanging(int value);
25
partial void OnpidChanged();
26
partial void OnpoidChanging(int value);
27
partial void OnpoidChanged();
28
partial void OnpNameChanging(string value);
29
partial void OnpNameChanged();
30
partial void OnpriceChanging(decimal value);
31
partial void OnpriceChanged();
32
partial void OnpdateChanging(System.DateTime value);
33
partial void OnpdateChanged();
34
#endregion
35
36
public product()
37
{
38
this._order = default(EntityRef<order>);
39
OnCreated();
40
}
41
42
[Column(Storage="_pid", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
43
[DataMember(Order=0)]
44
public int pid
45
{
46
get
47
{
48
return this._pid;
49
}
50
set
51
{
52
if ((this._pid != value))
53
{
54
this.OnpidChanging(value);
55
this.SendPropertyChanging();
56
this._pid = value;
57
this.SendPropertyChanged("pid");
58
this.OnpidChanged();
59
}
60
}
61
}
62
63
[Column(Storage="_poid", DbType="Int NOT NULL")]
64
[DataMember(Order = 1)]
65
public int poid
66
{
67
get
68
{
69
return this._poid;
70
}
71
set
72
{
73
if ((this._poid != value))
74
{
75
if (this._order.HasLoadedOrAssignedValue)
76
{
77
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
78
}
79
this.OnpoidChanging(value);
80
this.SendPropertyChanging();
81
this._poid = value;
82
this.SendPropertyChanged("poid");
83
this.OnpoidChanged();
84
}
85
}
86
}
87
88
[Column(Storage="_pName", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
89
[DataMember(Order = 2)]
90
public string pName
91
{
92
get
93
{
94
return this._pName;
95
}
96
set
97
{
98
if ((this._pName != value))
99
{
100
this.OnpNameChanging(value);
101
this.SendPropertyChanging();
102
this._pName = value;
103
this.SendPropertyChanged("pName");
104
this.OnpNameChanged();
105
}
106
}
107
}
108
109
[Column(Storage="_price", DbType="Decimal(18,2) NOT NULL")]
110
[DataMember(Order = 3)]
111
public decimal price
112
{
113
get
114
{
115
return this._price;
116
}
117
set
118
{
119
if ((this._price != value))
120
{
121
this.OnpriceChanging(value);
122
this.SendPropertyChanging();
123
this._price = value;
124
this.SendPropertyChanged("price");
125
this.OnpriceChanged();
126
}
127
}
128
}
129
130
[Column(Storage="_pdate", DbType="DateTime NOT NULL")]
131
[DataMember(Order = 4)]
132
public System.DateTime pdate
133
{
134
get
135
{
136
return this._pdate;
137
}
138
set
139
{
140
if ((this._pdate != value))
141
{
142
this.OnpdateChanging(value);
143
this.SendPropertyChanging();
144
this._pdate = value;
145
this.SendPropertyChanged("pdate");
146
this.OnpdateChanged();
147
}
148
}
149
}
150
151
[Association(Name="order_product", Storage="_order", ThisKey="poid", IsForeignKey=true)]
152
[DataMember(Order = 5)]
153
public order order
154
{
155
get
156
{
157
return this._order.Entity;
158
}
159
set
160
{
161
order previousValue = this._order.Entity;
162
if (((previousValue != value)
163
|| (this._order.HasLoadedOrAssignedValue == false)))
164
{
165
this.SendPropertyChanging();
166
if ((previousValue != null))
167
{
168
this._order.Entity = null;
169
previousValue.products.Remove(this);
170
}
171
this._order.Entity = value;
172
if ((value != null))
173
{
174
value.products.Add(this);
175
this._poid = value.oid;
176
}
177
else
178
{
179
this._poid = default(int);
180
}
181
this.SendPropertyChanged("order");
182
}
183
}
184
}
185
186
public event PropertyChangingEventHandler PropertyChanging;
187
188
public event PropertyChangedEventHandler PropertyChanged;
189
190
protected virtual void SendPropertyChanging()
191
{
192
if ((this.PropertyChanging != null))
193
{
194
this.PropertyChanging(this, emptyChangingEventArgs);
195
}
196
}
197
198
protected virtual void SendPropertyChanged(String propertyName)
199
{
200
if ((this.PropertyChanged != null))
201
{
202
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
203
}
204
}
205
}
Remarks类:

Code
1
[Table(Name="dbo.REMARK")]
2
[DataContract(Name = "Remarks", Namespace = "http://www.lgx.OrderLinq")]
3
public partial class REMARK : INotifyPropertyChanging, INotifyPropertyChanged
4
{
5
6
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
7
8
private int _RID;
9
10
private string _RNAME;
11
12
private int _RCID;
13
14
private string _RCONTENT;
15
16
private System.DateTime _RTIME;
17
18
Extensibility Method Definitions#region Extensibility Method Definitions
19
partial void OnLoaded();
20
partial void OnValidate(System.Data.Linq.ChangeAction action);
21
partial void OnCreated();
22
partial void OnRIDChanging(int value);
23
partial void OnRIDChanged();
24
partial void OnRNAMEChanging(string value);
25
partial void OnRNAMEChanged();
26
partial void OnRCIDChanging(int value);
27
partial void OnRCIDChanged();
28
partial void OnRCONTENTChanging(string value);
29
partial void OnRCONTENTChanged();
30
partial void OnRTIMEChanging(System.DateTime value);
31
partial void OnRTIMEChanged();
32
#endregion
33
34
public REMARK()
35
{
36
OnCreated();
37
}
38
39
[Column(Storage="_RID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
40
[DataMember(Order=0)]
41
public int RID
42
{
43
get
44
{
45
return this._RID;
46
}
47
set
48
{
49
if ((this._RID != value))
50
{
51
this.OnRIDChanging(value);
52
this.SendPropertyChanging();
53
this._RID = value;
54
this.SendPropertyChanged("RID");
55
this.OnRIDChanged();
56
}
57
}
58
}
59
60
[Column(Storage="_RNAME", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
61
[DataMember(Order = 1)]
62
public string RNAME
63
{
64
get
65
{
66
return this._RNAME;
67
}
68
set
69
{
70
if ((this._RNAME != value))
71
{
72
this.OnRNAMEChanging(value);
73
this.SendPropertyChanging();
74
this._RNAME = value;
75
this.SendPropertyChanged("RNAME");
76
this.OnRNAMEChanged();
77
}
78
}
79
}
80
81
[Column(Storage="_RCID", DbType="Int NOT NULL")]
82
[DataMember(Order = 2)]
83
public int RCID
84
{
85
get
86
{
87
return this._RCID;
88
}
89
set
90
{
91
if ((this._RCID != value))
92
{
93
this.OnRCIDChanging(value);
94
this.SendPropertyChanging();
95
this._RCID = value;
96
this.SendPropertyChanged("RCID");
97
this.OnRCIDChanged();
98
}
99
}
100
}
101
102
[Column(Storage="_RCONTENT", DbType="VarChar(200) NOT NULL", CanBeNull=false)]
103
[DataMember(Order = 3)]
104
public string RCONTENT
105
{
106
get
107
{
108
return this._RCONTENT;
109
}
110
set
111
{
112
if ((this._RCONTENT != value))
113
{
114
this.OnRCONTENTChanging(value);
115
this.SendPropertyChanging();
116
this._RCONTENT = value;
117
this.SendPropertyChanged("RCONTENT");
118
this.OnRCONTENTChanged();
119
}
120
}
121
}
122
123
[Column(Storage="_RTIME", DbType="DateTime NOT NULL")]
124
[DataMember(Order = 4)]
125
public System.DateTime RTIME
126
{
127
get
128
{
129
return this._RTIME;
130
}
131
set
132
{
133
if ((this._RTIME != value))
134
{
135
this.OnRTIMEChanging(value);
136
this.SendPropertyChanging();
137
this._RTIME = value;
138
this.SendPropertyChanged("RTIME");
139
this.OnRTIMEChanged();
140
}
141
}
142
}
143
144
public event PropertyChangingEventHandler PropertyChanging;
145
146
public event PropertyChangedEventHandler PropertyChanged;
147
148
protected virtual void SendPropertyChanging()
149
{
150
if ((this.PropertyChanging != null))
151
{
152
this.PropertyChanging(this, emptyChangingEventArgs);
153
}
154
}
155
156
protected virtual void SendPropertyChanged(String propertyName)
157
{
158
if ((this.PropertyChanged != null))
159
{
160
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
161
}
162
}
163
}
其次,建立WCF服务

Code
1
[ServiceContract(Name = "IOrderService_Contract", Namespace = "http://www.lgx.OrderService/IOrderService_Contract/", SessionMode = SessionMode.Required)]
2
3
public interface IOrderService
4
{
5
[OperationContract(Name = "SaveOrder_Contract", Action = "http://www.lgx.OrderService/IOrderService/SaveOrder_Contract/", ReplyAction = "http://www.lgx.OrderService/IOrderService/SaveOrder_ContractResponse")]
6
[ServiceKnownType(typeof(OrderLinq.product))]
7
void SaveOrder(OrderLinq.order ord);
8
[OperationContract(Name = "GetEnt_Contract", Action = "http://www.lgx.OrderService/IOrderService/GetEnt_Contract/", ReplyAction = "http://www.lgx.OrderService/IOrderService/GetEnt_ContractResponse")]
9
Ent GetEnt();
10
[OperationContract(Name = "GetOrder_Contract", Action = "http://www.lgx.OrderService/IOrderService/GetOrder_Contract/", ReplyAction = "http://www.lgx.OrderService/IOrderService/GetOrder_ContractResponse")]
11
OrderLinq.order GetOrder();
12
13
[OperationContract(Name = "GetProduct_Contract", Action = "http://www.lgx.OrderService/IOrderService/GetProduct_Contract/", ReplyAction = "http://www.lgx.OrderService/IOrderService/GetProduct_ContractResponse")]
14
product GetProduct();
15
16
[OperationContract(Name = "SaveRemak_Contract", Action = "http://www.lgx.OrderService/IOrderService/SaveRemak_Contract/", ReplyAction = "http://www.lgx.OrderService/IOrderService/SaveRemak_ContractResponse")]
17
void SaveRemak(REMARK remark);
18
19
}
建立客户端并进行远程引用和调用:

Code
1
static void Main(string[] args)
2
{
3
//初始化属性
4
Remarks remark = new Remarks();
5
remark.RCID = 1;
6
remark.RCONTENT = "顶一下";
7
remark.RNAME = "五哥";
8
remark.RTIME = DateTime.Now;
9
10
//初始化属性
11
Orders orders = new Orders();
12
orders.odate = DateTime.Now;
13
orders.totalPrice = Convert.ToDecimal(0.0f);
14
15
16
OrderService_ContractClient clients = new OrderService_ContractClient();
17
//在这里操作评论表插入数据没有问题
18
clients.SaveRemak_Contract(remark);
19
//插入订单时提示EntitySet<product>为空,因为有主外键关系映射
20
clients.SaveOrder_Contract(orders);
21
异常

Code
1
2
//这里提示EntitySet<product>为空,不知为何,
3
//如果数据库表没有主外键映射则不会有这个问题
4
//希望大家帮忙解决,实在是感激不尽呀!!
5
[Association(Name="order_product", Storage="_products", OtherKey="poid")]
6
[DataMember(Order = 3)]
7
public EntitySet<product> products
8
{
9
get
10
{
11
return this._products;
12
}
13
set
14
{
15
this._products.Assign(value);
16
}
17
}