Guest User

Untitled

a guest
Aug 1st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. EF Inserts already existing item
  2. protected void Application_BeginRequest(Object sender, EventArgs e)
  3. {
  4. //-- Create an instance of EntityContext
  5. HttpContext.Current.Items[Constants.ENTITYCONTEXT] = new EntityContext();
  6. }
  7. protected void Application_EndRequest(Object sender, EventArgs e)
  8. {
  9. //-- Clean up the entitycontext
  10. var entityContext = HttpContext.Current.Items[Constants.ENTITYCONTEXT] as EntityContext;
  11. if (entityContext != null)
  12. entityContext.Dispose();
  13. }
  14.  
  15. //-- EntityContext
  16. private EntityContext CurrentContext
  17. {
  18. get { return HttpContext.Current.Items[Constants.ENTITYCONTEXT] as EntityContext; }
  19. }
  20.  
  21. private Domain.Model.Club LoadClubByIdentifier(Guid identifier)
  22. {
  23. return this.CurrentContext.Clubs.SingleOrDefault(c => c.Identifier == identifier);
  24. }
  25.  
  26. Domain.Model.User user = Mapper.Map<Web.Content.Code.Models.User.CreatePlayer, Domain.Model.User>(model);
  27. user.Identifier = new Guid().NewSequentialGuid();
  28.  
  29. //-- Get current club
  30. user.Clubs.Add(base.CurrentClub);
  31.  
  32. //-- Get all teams and add them to user
  33. foreach (string teamIdentifier in model.Team)
  34. {
  35. Domain.Model.Team team = new Domain.Services.TeamServices().LoadByIdentifier(
  36. Domain.Helper.CryptationHelper.DecryptIdentifier(teamIdentifier));
  37.  
  38. user.Teams.Add(new TeamUser() { Team = team, User = user, UserTeamRelation = UserTeamRelationEnum.Player });
  39. }
  40.  
  41. //-- Create this user
  42. new Domain.Services.UserServices().CreatePlayer(user);
  43.  
  44. public void CreatePlayer(Domain.Model.User user)
  45. {
  46. Password password = Password.GenerateRandomPassword();
  47. user.Password = password;
  48.  
  49. //-- Save the user to storage
  50. _userRepository.SaveUser(user);
  51. }
  52.  
  53. //-- Methods
  54. private void SaveUser(Domain.Model.User user)
  55. {
  56. this.CurrentContext.Users.Add(user);
  57. this.CurrentContext.SaveChanges();
  58. }
Add Comment
Please, Sign In to add comment