diff --git a/Project-Unite/Models/IdentityModels.cs b/Project-Unite/Models/IdentityModels.cs index 8959a8e..3d55b2f 100644 --- a/Project-Unite/Models/IdentityModels.cs +++ b/Project-Unite/Models/IdentityModels.cs @@ -76,9 +76,19 @@ public Role HighestRole var roleList = new List(); foreach (var role in this.Roles) { - roleList.Add(new ApplicationDbContext().Roles.First(r => r.Id == role.RoleId) as Role); + var found = (new ApplicationDbContext().Roles.FirstOrDefault(r => r.Id == role.RoleId) as Role); + if (found != null) + roleList.Add(found); } + if(roleList.Count()==0) + { + var roles = new List(); + var db = new ApplicationDbContext(); + foreach (var r in db.Roles.ToArray()) + roles.Add(r as Role); + return roles.OrderBy(x => x.Priority).First(); + } return roleList.OrderByDescending(x => x.Priority).First(); } }