Entity Framework Loading Related Entities[
^]
You have two options:
1) Lazy Loading (Not recommended)
Make your collection and navigation properties
virtual
, and Entity Framework will create dynamic proxies for your entities, which will be able to load the related entities dynamically on demand.
public virtual Role Role { get; set; }
However, this only works so long as the
DbContext
is still alive, and it can dramatically increase the number of queries used.
2) Eager Loading
Use the
Include
extension method to load the related entities at the same time as the main entity.
List<User> uList = uc.Users
.Include(p => p.Role)
.Where(p => p.UserName == u.UserName && p.UserPwd == u.UserPwd)
.ToList();
NB: You are storing passwords in plain text. Don't do that!
Secure Password Authentication Explained Simply[
^]
Salted Password Hashing - Doing it Right[
^]
And why are you re-inventing the wheel? ASP.NET has several perfectly good authentication systems built-in - for example,
ASP.NET Identity[
^]