一直以来都在学习中,所以也没有什么好文章献给大家,这次小弟也是来提问的,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
}