@@ -105,12 +105,30 @@ public MongoOid GenerateId() {
105
105
/// The Oid for a document (if any)
106
106
/// </summary>
107
107
public MongoOid Id {
108
- get { return this . Get < MongoOid > ( Mongo . DocumentIdKey , new MongoOid ( ) ) ; }
108
+ get {
109
+
110
+ //if no id has been generated, do it now
111
+ MongoOid id = this . Get < MongoOid > ( Mongo . DocumentIdKey , null ) ;
112
+ if ( id == null ) {
113
+ id = new MongoOid ( ) ;
114
+ this . Set ( Mongo . DocumentIdKey , id ) ;
115
+ }
116
+
117
+ //return the id to use
118
+ return id ;
119
+
120
+ }
109
121
set {
122
+
123
+ //if trying to assign anything that isn't a MongoOid
124
+ //then complain about it
110
125
if ( value != null && ! ( value is MongoOid ) ) {
111
126
throw new ArgumentException ( "You can only assign a MongoOid as the Id for a MongoDocument" ) ;
112
127
}
128
+
129
+ //update the value
113
130
this . Set < MongoOid > ( Mongo . DocumentIdKey , value ) ;
131
+
114
132
}
115
133
}
116
134
@@ -129,14 +147,14 @@ protected override IEnumerable<BsonFieldDetail> OnBeforeFinishBsonRender(IEnumer
129
147
//get all of the ids to use
130
148
IEnumerable < BsonFieldDetail > ids = fields . Where ( item => item . Type is MongoOidType ) ;
131
149
list . RemoveAll ( item => item . Type is MongoOidType ) ;
150
+ list = list . OrderBy ( item => item . Length ) . ToList ( ) ;
132
151
133
152
//because the items are shared in the same list
134
153
//we have to insert these one at the time since
135
154
//if we use AddRange an exception will be thrown
136
155
//since the list is modified while enumerating
137
156
//through the values. We're also going backwards
138
157
//to make sure they retain their original order
139
- list = list . OrderBy ( item => item . Length ) . ToList ( ) ;
140
158
for ( int index = ids . Count ( ) ; index -- > 0 ; ) {
141
159
list . Insert ( 0 , ids . ElementAt ( index ) ) ;
142
160
}
0 commit comments