You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current scaffolded code first tries to find the resouce (in a tracking way) and uses a different model to update the resource. Since the model updated is different from the model added to the context, the PUT operation fails.
The find operation needs to change to a non-tracking form: model.AsNoTracking().FirstOrDefaultAsync(r => r.Id == Id)
Based on https://www.ietf.org/rfc/rfc2616.txt, the PUT operation is not required to check for the existence of the resource. If the provided model matches an existing resource it will be updated, else a new resource should be created.
If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.
Preference:
Since the API controller scaffolders follow 2, it would be preferable to match the behavior for both API end-point and API controller scaffolders.
Thanks @timheuer for identifying and tracking down the issue.
@DamianEdwards - Do you have a preference on how these should be updated to handle the error scenario here?
#2269 addresses a lot of the concerns but does not address If the provided model matches an existing resource it will be updated, else a new resource should be created. in the fix suggested above. Any thoughts on that @DamianEdwards@sayedihashimi.
Issue:
The current scaffolded code first tries to find the resouce (in a tracking way) and uses a different model to update the resource. Since the model updated is different from the model added to the context, the PUT operation fails.
Code block with the issue:
https://github.com/dotnet/Scaffolding/blob/main/src/Scaffolding/VS.Web.CG.Mvc/Templates/MinimalApi/MinimalApiEf.cshtml#L98-L110
Fix
There are 2 ways to fix this issue:
The find operation needs to change to a non-tracking form:
model.AsNoTracking().FirstOrDefaultAsync(r => r.Id == Id)
Based on https://www.ietf.org/rfc/rfc2616.txt, the PUT operation is not required to check for the existence of the resource. If the provided model matches an existing resource it will be updated, else a new resource should be created.
Preference:
Since the API controller scaffolders follow 2, it would be preferable to match the behavior for both API end-point and API controller scaffolders.
Thanks @timheuer for identifying and tracking down the issue.
@DamianEdwards - Do you have a preference on how these should be updated to handle the error scenario here?
Related issue: #1964
The text was updated successfully, but these errors were encountered: