Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everyone,

This is the first time i am creating something on ASP.net MVC with entity framework with code first Approach, User details are getting loaded but role for the user is not getting loaded my code is as follows.

What I have tried:

public class User
 {
     public int UserId { get; set; }
     [Required]
     public string UserName { get; set; }

     [Required]
     public string UserPwd { get; set; }
     public string ProjectCode { get; set; }
     public string UserType { get; set; }
     public bool IsEnabled{ get; set; }
     //public int RoleId { get; set; }

     public DateTime? CreatedDate { get; set; }
     public string CreatedBy { get; set; }
     public DateTime? UpdateDate { get; set; }
     public string UpdatedBy { get; set; }
     public Role Role { get; set; }

 }


public class Role
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }

    public ICollection<User> Users { get; set; }
}


public class UserContext : DbContext
{
    public DbSet<Role> Roles { get; set; }
    public DbSet<User> Users { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer<UserContext>(null);
        modelBuilder.Entity<User>().ToTable("Users");


        modelBuilder.Entity<User>().HasKey(u=> new{ u.UserId});

    }

}


public static bool Authenticate(User u)
{
    UserContext uc = new UserContext();

    List<User> uList = uc.Users.Where(p => p.UserName == u.UserName && p.UserPwd == u.UserPwd).ToList();


    return false;
}


Above uList returns Users list but not Roles??
Not able to understand what to do, tried many articles on internet but nothing is really working for me.
Posted

1 solution

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.
C#
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.
C#
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[^]
 
Share this answer
 
Comments
Karthik_Mahalingam 20-Nov-17 20:54pm    
5
sunil mali 21-Nov-17 1:50am    
Thank you sir,
Lazy loading worked perfectly in my case.
I tried eager loading but it gave error on line .Include(p => p.Role) (Cannot convert lambda expression to type string because it is not a delegate type.)
Can you please help.

why are you re-inventing the wheel? ASP.NET has several perfectly good authentication systems built-in - for example,
[Sunil]This is just a demo project which i want to try before starting with the Actual project so that i don't get stuck when i am working on real environment.
sunil mali 21-Nov-17 2:00am    
I am done the error while eager loading was because of namespace(System.Data.Entity)
Thank you so much Richard your Answer was very helpful.
Also references suggested by you were very useful. Much Appreciated, Thank you once again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900