Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data.Entity;
- using GAP.Domain;
- using Microsoft.Practices.Unity;
- using GAP.Services;
- namespace GAP {
- public class GapContext : DbContext {
- public DbSet<User> Users { get; set; }
- public DbSet<Role> Roles { get; set; }
- public GapContext()
- : base("ExpertSite") {
- }
- public virtual void Commit() {
- base.SaveChanges();
- }
- protected override void OnModelCreating(DbModelBuilder modelBuilder) {
- //base.OnModelCreating(modelBuilder);
- modelBuilder.Entity<User>()
- .HasMany(u => u.Roles)
- .WithMany(r => r.Users)
- .Map(m => {
- m.ToTable("UserRoles");
- });
- }
- }
- public class GapContextInitialiser : DropCreateDatabaseAlways<GapContext> {
- protected override void Seed(GapContext context) {
- context.Configuration.LazyLoadingEnabled = true;
- Role role = context.Roles.Add(new Role { RoleName = "admin" });
- User user = new User {
- // UserId = Guid.NewGuid(),
- Email = "olli.holliday@gmail.com",
- HashedPassword = GAP.Services.Cryptography.HashPassword("hellosir"),
- EmailIsConfirmed = true,
- };
- context.Users.Add(user);
- //user.Roles.Add(new Role { RoleName = "admin" });
- context.SaveChanges();
- if (context.Users.First().Roles == null) {
- // this will be hit......
- // but it should be an empty ICollection
- }
- /* fix */
- context = new GapContext();
- if (context.Users.First().Roles == null) {
- // this will not hit!
- }
- context.Users.Where(u => u.EmailIsConfirmed == true).FirstOrDefault().Roles.Add(role);
- context.SaveChanges();
- /* fix */
- //
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement