I have an Action that requires authentication. The action method creates some records with foreign keys using Linq to SQL. When the user calls the action and is logged in the method works without a problem. When the user is not logged in, MVC redirects them to the login page with the returnUrl parameter. After a successful login the action method is executed, but this time it throws the following error:
An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported.
All queries are using the same datacontext and on the same request.
The code goes through the same execution path in both scenarios so I trully don't understand what's going on and why? Does anyone have any idea why this would happen?