Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data.Entity;
  6. using GAP.Domain;
  7. using Microsoft.Practices.Unity;
  8. using GAP.Services;
  9.  
  10. namespace GAP {
  11.  
  12.     public class GapContext : DbContext {
  13.  
  14.         public DbSet<User> Users { get; set; }
  15.         public DbSet<Role> Roles { get; set; }
  16.  
  17.         public GapContext()
  18.             : base("ExpertSite") {
  19.  
  20.            
  21.         }
  22.  
  23.         public virtual void Commit() {
  24.             base.SaveChanges();
  25.         }
  26.  
  27.         protected override void OnModelCreating(DbModelBuilder modelBuilder) {
  28.  
  29.             //base.OnModelCreating(modelBuilder);
  30.             modelBuilder.Entity<User>()
  31.                .HasMany(u => u.Roles)
  32.                .WithMany(r => r.Users)
  33.                .Map(m => {
  34.                    m.ToTable("UserRoles");
  35.                });
  36.  
  37.         }
  38.  
  39.     }
  40.  
  41.  
  42.     public class GapContextInitialiser : DropCreateDatabaseAlways<GapContext> {
  43.  
  44.  
  45.         protected override void Seed(GapContext context) {
  46.  
  47.  
  48.             context.Configuration.LazyLoadingEnabled = true;
  49.  
  50.  
  51.             Role role = context.Roles.Add(new Role { RoleName = "admin" });
  52.  
  53.             User user = new User {
  54.                 //                UserId = Guid.NewGuid(),
  55.                 Email = "olli.holliday@gmail.com",
  56.                 HashedPassword = GAP.Services.Cryptography.HashPassword("hellosir"),
  57.                 EmailIsConfirmed = true,
  58.             };
  59.             context.Users.Add(user);
  60.  
  61.             //user.Roles.Add(new Role { RoleName = "admin" });
  62.  
  63.             context.SaveChanges();
  64.  
  65.             if (context.Users.First().Roles == null) {
  66.                 // this will be hit......
  67.                 // but it should be an empty ICollection
  68.             }
  69.  
  70.             /* fix */
  71.  
  72.             context = new GapContext();
  73.  
  74.             if (context.Users.First().Roles == null) {
  75.                 // this will not hit!                
  76.             }
  77.  
  78.             context.Users.Where(u => u.EmailIsConfirmed == true).FirstOrDefault().Roles.Add(role);
  79.             context.SaveChanges();
  80.  
  81.             /* fix */
  82.  
  83.  
  84.             //
  85.         }
  86.  
  87.     }
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement