-
-
Notifications
You must be signed in to change notification settings - Fork 158
fix/#445 #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix/#445 #446
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e651a3e
add rider to ignore
roblankey b9923f3
update git ignore to ignore all rider
roblankey b395945
change Type to ResourceType and add EntityType that defaults to Resou…
roblankey 649648f
bump package version?
roblankey a7a22bb
re-change the typing since it was causing issues when the entity had …
roblankey 0938c33
add rider to ignore
roblankey d513658
update git ignore to ignore all rider
roblankey 3233869
change Type to ResourceType and add EntityType that defaults to Resou…
roblankey f1a0cf6
bump package version?
roblankey 6bc8187
re-change the typing since it was causing issues when the entity had …
roblankey 135853b
Merge branch 'fix/#445' of github.com:roblankey/JsonApiDotNetCore int…
roblankey 5ee3068
add entity check to hasmany
roblankey bf3cec4
fix copy pasta
roblankey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,3 +234,6 @@ _Pvt_Extensions | |
|
||
# FAKE - F# Make | ||
.fake/ | ||
|
||
### Rider ### | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,24 +155,46 @@ public virtual async Task<TEntity> CreateAsync(TEntity entity) | |
protected virtual void AttachRelationships(TEntity entity = null) | ||
{ | ||
AttachHasManyPointers(entity); | ||
AttachHasOnePointers(); | ||
AttachHasOnePointers(entity); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void DetachRelationshipPointers(TEntity entity) | ||
{ | ||
foreach (var hasOneRelationship in _jsonApiContext.HasOneRelationshipPointers.Get()) | ||
{ | ||
_context.Entry(hasOneRelationship.Value).State = EntityState.Detached; | ||
var hasOne = (HasOneAttribute) hasOneRelationship.Key; | ||
if (hasOne.EntityPropertyName != null) | ||
{ | ||
var relatedEntity = entity.GetType().GetProperty(hasOne.EntityPropertyName)?.GetValue(entity); | ||
if (relatedEntity != null) | ||
_context.Entry(relatedEntity).State = EntityState.Detached; | ||
} | ||
else | ||
{ | ||
_context.Entry(hasOneRelationship.Value).State = EntityState.Detached; | ||
} | ||
} | ||
|
||
foreach (var hasManyRelationship in _jsonApiContext.HasManyRelationshipPointers.Get()) | ||
{ | ||
foreach (var pointer in hasManyRelationship.Value) | ||
var hasMany = (HasManyAttribute) hasManyRelationship.Key; | ||
if (hasMany.EntityPropertyName != null) | ||
{ | ||
_context.Entry(pointer).State = EntityState.Detached; | ||
var relatedList = (IList)entity.GetType().GetProperty(hasMany.EntityPropertyName)?.GetValue(entity); | ||
foreach (var related in relatedList) | ||
{ | ||
_context.Entry(related).State = EntityState.Detached; | ||
} | ||
} | ||
|
||
else | ||
{ | ||
foreach (var pointer in hasManyRelationship.Value) | ||
{ | ||
_context.Entry(pointer).State = EntityState.Detached; | ||
} | ||
} | ||
|
||
// HACK: detaching has many relationships doesn't appear to be sufficient | ||
// the navigation property actually needs to be nulled out, otherwise | ||
// EF adds duplicate instances to the collection | ||
|
@@ -192,14 +214,27 @@ private void AttachHasManyPointers(TEntity entity) | |
if (relationship.Key is HasManyThroughAttribute hasManyThrough) | ||
AttachHasManyThrough(entity, hasManyThrough, relationship.Value); | ||
else | ||
AttachHasMany(relationship.Key as HasManyAttribute, relationship.Value); | ||
AttachHasMany(entity, relationship.Key as HasManyAttribute, relationship.Value); | ||
} | ||
} | ||
|
||
private void AttachHasMany(HasManyAttribute relationship, IList pointers) | ||
private void AttachHasMany(TEntity entity, HasManyAttribute relationship, IList pointers) | ||
{ | ||
foreach (var pointer in pointers) | ||
_context.Entry(pointer).State = EntityState.Unchanged; | ||
if (relationship.EntityPropertyName != null) | ||
{ | ||
var relatedList = (IList)entity.GetType().GetProperty(relationship.EntityPropertyName)?.GetValue(entity); | ||
foreach (var related in relatedList) | ||
{ | ||
_context.Entry(related).State = EntityState.Unchanged; | ||
} | ||
} | ||
else | ||
{ | ||
foreach (var pointer in pointers) | ||
{ | ||
_context.Entry(pointer).State = EntityState.Unchanged; | ||
} | ||
} | ||
} | ||
|
||
private void AttachHasManyThrough(TEntity entity, HasManyThroughAttribute hasManyThrough, IList pointers) | ||
|
@@ -227,12 +262,27 @@ private void AttachHasManyThrough(TEntity entity, HasManyThroughAttribute hasMan | |
/// This is used to allow creation of HasOne relationships when the | ||
/// independent side of the relationship already exists. | ||
/// </summary> | ||
private void AttachHasOnePointers() | ||
private void AttachHasOnePointers(TEntity entity) | ||
{ | ||
var relationships = _jsonApiContext.HasOneRelationshipPointers.Get(); | ||
foreach (var relationship in relationships) | ||
if (_context.Entry(relationship.Value).State == EntityState.Detached && _context.EntityIsTracked(relationship.Value) == false) | ||
_context.Entry(relationship.Value).State = EntityState.Unchanged; | ||
{ | ||
if (relationship.Key.GetType() != typeof(HasOneAttribute)) | ||
continue; | ||
|
||
var hasOne = (HasOneAttribute) relationship.Key; | ||
if (hasOne.EntityPropertyName != null) | ||
{ | ||
var relatedEntity = entity.GetType().GetProperty(hasOne.EntityPropertyName)?.GetValue(entity); | ||
if (relatedEntity != null && _context.Entry(relatedEntity).State == EntityState.Detached && _context.EntityIsTracked((IIdentifiable)relatedEntity) == false) | ||
_context.Entry(relatedEntity).State = EntityState.Unchanged; | ||
} | ||
else | ||
{ | ||
if (_context.Entry(relationship.Value).State == EntityState.Detached && _context.EntityIsTracked(relationship.Value) == false) | ||
_context.Entry(relationship.Value).State = EntityState.Unchanged; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's refactor this a bit so it's clearer and we don't have separate definitions for how the EF ChangeTracker entry state is modified. The only thing that needs to be in the conditional is how we determine the IIdentifiable relatedEntity = (hasOne.EntityPropertyName != null)
? entity.GetType().GetProperty(hasOne.EntityPropertyName)?.GetValue(entity) as IIdentifiable
: relationship.Value;
if(relatedEntity != null
&& _context.Entry(relatedEntity).State == EntityState.Detached
&& _context.EntityIsTracked(relatedEntity) == false) {
_context.Entry(relatedEntity).State = EntityState.Unchanged;
} |
||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under what circumstances does this happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is totally just a guard clause since the dictionary key is RelationshipAttribute.