I'm trying to figure out how can i retrieve roles with Any and check if user exists on the same query and return as SingleOrDefault() but doesn't seem to work.
private User Authenticate(LoginViewModel model)
{
using (HutLogisticaContext context = new HutLogisticaContext())
{
return context.Users.Where(u => u.Username.Contains(model.Username)).SingleOrDefault();
}
}
However it won't let met add single or default if i add Any. How can i do both on same query?
EDIT
I forgot to mention but I want to return the user object with all the roles related to it, not sure if it's possible without navigation property because i could not use include
public class Role
{
public int Id { get; set; }
public string Nome { get; set; }
public ICollection<User> Users { get; set; }
public ICollection<Claim> Claims { get; set; }
}
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public DateTime? DataSessao { get; set; }
public bool IsActivo { get; set; }
public virtual Colaborador Colaborador { get; set; }
public ICollection<Role> Roles { get; set; }
public ICollection<App> Apps { get; set; }
}
i'm using EF 6 so this created automatically the join table with UserId and RoleId as PK and FK