实体框架无法在一对多中加载角色 [英] Entity framework not able load roles in one to many

查看:85
本文介绍了实体框架无法在一对多中加载角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,



这是我第一次在ASP.net MVC上创建一些带有代码的实体框架的方法第一种方法,用户详细信息正在加载但是用户的角色没有加载我的代码如下。



我尝试过:



公共类用户
{
public int UserId {get;组; }
[必需]
公共字符串UserName {get;组; }

[必需]
公共字符串UserPwd {get;组; }
公共字符串ProjectCode {get;组; }
公共字符串UserType {get;组; }
public bool IsEnabled {get;组; }
// public int RoleId {get;组; }

公共日期时间? CreatedDate {get;组; }
公共字符串CreatedBy {get;组; }
public DateTime? UpdateDate {get;组; }
公共字符串UpdatedBy {get;组; }
public Role Role {get;组; }

}





公共类角色
{
public int RoleId {get;组; }
公共字符串RoleName {get;组; }

public ICollection< User>用户{get;组; }
}





公共类UserContext:DbContext 
{
public DbSet< Role>角色{得到;组; }
public DbSet< User>用户{get;组; }
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();

列表<用户> uList = uc.Users.Where(p => p.UserName == u.UserName&& p.UserPwd == u.UserPwd).ToList();


返回false;
}





以上uList返回用户列表但不是角色?

无法理解该做什么,在互联网上尝试过很多文章,但没有什么对我有用。

解决方案

实体框架加载相关实体 [ ^ ]



你有两个选项:



1)延迟加载 (不推荐)

制作你的收集和导航属性虚拟,实体框架将为您的实体创建动态代理,这些代理将能够根据需要动态加载相关实体。

  public   virtual 角色角色{获得;  set ; } 



但是,这只有在 DbContext 仍然存在时才有效,并且可以大大增加数量使用的查询。



2)渴望加载

使用包含与主实体同时加载相关实体的扩展方法。

列表<用户> uList = uc.Users 
.Include(p = > p.Role)
.Where(p = > p.UserName == u.UserName&& p.UserPwd == u.UserPwd)
.ToList();







注意:您以纯文本格式存储密码。不要这样做!

安全密码认证简单解释 [ ^ ]

Salted Password Hashing - 正确行事 [ ^ ]



你为什么要重新发明轮子? ASP.NET内置了几个非常好的身份验证系统 - 例如, ASP.NET身份 [ ^ ]


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.

解决方案

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[^]


这篇关于实体框架无法在一对多中加载角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆