Esempio n. 1
0
        private static void SeedData(HiggsDbContext context)
        {
            if (!context.AllMigrationsApplied())
            {
                return;
            }

            var seedUsers = new[] { new SeedUser {
                                        AccountId = RobAccountId, UserName = "******"
                                    } };
            var dbScopes = context.Scopes.ToList().ToDictionary(p => p.Name, p => p, StringComparer.OrdinalIgnoreCase);

            // Insert new scopes
            foreach (var scope in Scopes.AllScopes)
            {
                if (!dbScopes.ContainsKey(scope.Key))
                {
                    context.Scopes.Add(new DbScope {
                        Name = scope.Key, Description = scope.Value
                    });
                }
            }

            // Delete old scopes
            foreach (var dbScope in dbScopes)
            {
                if (!Scopes.AllScopes.ContainsKey(dbScope.Key))
                {
                    context.Scopes.Remove(dbScope.Value);
                }
            }

            foreach (var seedUser in seedUsers)
            {
                var existingUser = context.Users.Include(u => u.UserScopes)
                                   .FirstOrDefault(u => u.AccountId == seedUser.AccountId);
                if (existingUser == null)
                {
                    existingUser = new DbUser {
                        AccountId = seedUser.AccountId, Name = seedUser.UserName
                    };
                    context.Users.Add(existingUser);
                }

                if (existingUser.UserScopes == null)
                {
                    existingUser.UserScopes = new List <DbUserScope>();
                }

                foreach (var scope in Scopes.AllScopes)
                {
                    if (existingUser.UserScopes.All(s => !string.Equals(s.ScopeName, scope.Key, StringComparison.OrdinalIgnoreCase)))
                    {
                        context.UserScopes.Add(new DbUserScope {
                            UserId = seedUser.AccountId, ScopeName = scope.Key
                        });
                    }
                }
            }

            context.SaveChanges();
        }
Esempio n. 2
0
 public static void Setup(this HiggsDbContext context)
 {
     SeedData(context);
 }