|
1 | 1 | using System; |
| 2 | +using System.Collections; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.Linq; |
4 | 5 | using System.Threading.Tasks; |
@@ -143,17 +144,17 @@ public virtual async Task<TEntity> GetAndIncludeAsync(TId id, string relationshi |
143 | 144 | /// <inheritdoc /> |
144 | 145 | public virtual async Task<TEntity> CreateAsync(TEntity entity) |
145 | 146 | { |
146 | | - AttachRelationships(); |
| 147 | + AttachRelationships(entity); |
147 | 148 | _dbSet.Add(entity); |
148 | 149 |
|
149 | 150 | await _context.SaveChangesAsync(); |
150 | 151 |
|
151 | 152 | return entity; |
152 | 153 | } |
153 | 154 |
|
154 | | - protected virtual void AttachRelationships() |
| 155 | + protected virtual void AttachRelationships(TEntity entity = null) |
155 | 156 | { |
156 | | - AttachHasManyPointers(); |
| 157 | + AttachHasManyPointers(entity); |
157 | 158 | AttachHasOnePointers(); |
158 | 159 | } |
159 | 160 |
|
@@ -183,15 +184,42 @@ public void DetachRelationshipPointers(TEntity entity) |
183 | 184 | /// This is used to allow creation of HasMany relationships when the |
184 | 185 | /// dependent side of the relationship already exists. |
185 | 186 | /// </summary> |
186 | | - private void AttachHasManyPointers() |
| 187 | + private void AttachHasManyPointers(TEntity entity) |
187 | 188 | { |
188 | 189 | var relationships = _jsonApiContext.HasManyRelationshipPointers.Get(); |
189 | 190 | foreach (var relationship in relationships) |
190 | 191 | { |
191 | | - foreach (var pointer in relationship.Value) |
192 | | - { |
193 | | - _context.Entry(pointer).State = EntityState.Unchanged; |
194 | | - } |
| 192 | + if(relationship.Key is HasManyThroughAttribute hasManyThrough) |
| 193 | + AttachHasManyThrough(entity, hasManyThrough, relationship.Value); |
| 194 | + else |
| 195 | + AttachHasMany(relationship.Key as HasManyAttribute, relationship.Value); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private void AttachHasMany(HasManyAttribute relationship, IList pointers) |
| 200 | + { |
| 201 | + foreach (var pointer in pointers) |
| 202 | + { |
| 203 | + _context.Entry(pointer).State = EntityState.Unchanged; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private void AttachHasManyThrough(TEntity entity, HasManyThroughAttribute hasManyThrough, IList pointers) |
| 208 | + { |
| 209 | + // create the collection (e.g. List<ArticleTag>) |
| 210 | + // this type MUST implement IList so we can build the collection |
| 211 | + // if this is problematic, we _could_ reflect on the type and find an Add method |
| 212 | + // or we might be able to create a proxy type and implement the enumerator |
| 213 | + var throughRelationshipCollection = Activator.CreateInstance(hasManyThrough.ThroughProperty.PropertyType) as IList; |
| 214 | + hasManyThrough.ThroughProperty.SetValue(entity, throughRelationshipCollection); |
| 215 | + foreach (var pointer in pointers) |
| 216 | + { |
| 217 | + _context.Entry(pointer).State = EntityState.Unchanged; |
| 218 | + |
| 219 | + var throughInstance = Activator.CreateInstance(hasManyThrough.ThroughType); |
| 220 | + hasManyThrough.LeftProperty.SetValue(throughInstance, entity); |
| 221 | + hasManyThrough.RightProperty.SetValue(throughInstance, pointer); |
| 222 | + throughRelationshipCollection.Add(throughInstance); |
195 | 223 | } |
196 | 224 | } |
197 | 225 |
|
|
0 commit comments