【问题标题】:How to implement a many to many relationship in ASP.NET using SQL Server如何使用 SQL Server 在 ASP.NET 中实现多对多关系
【发布时间】:2016-04-03 11:41:57
【问题描述】:

基本上,我试图在 SQL Server 2010 的中间表中插入一个 userId 值和一个 RoleId 值。问题是代码没有到达表并且它保持为空,而使用断点,我可以看到分配角色的值是正确的。我正在使用 ASP.NET MVC 5 作为学校项目,我试图在网站中为用户提供不同权限的角色。

设置角色的方法在业务层:

public void RegisterUser(CommonLayer.User User, string ConfirmPassword)
{
    CommonLayer.User Existing = this.GetUser(User.UserEmail);
    BuisnessLayer.Roles roles = new BuisnessLayer.Roles();

    if (Existing == null)
    {
        if (User.UserPassword.Equals(ConfirmPassword))
        {
            User.UserId = Guid.NewGuid();
            User.UserPassword = HashSHA512String(User.UserPassword, User.UserId.ToString());
            User.UserTypeId = (1);
            this.AddUser(User);
            roles.AddUserRole("USR",User.UserId);
        }
    }
}

AddUserRole 似乎是问题所在,因为它没有在表中插入业务逻辑中AddUserRole 的方法是:

public void AddUserRole(string RoleCode, Guid UserId)
{
    DataLayer.DARoles dar = new DataLayer.DARoles(this.entities);
    DataLayer.DAUsers dau = new DataLayer.DAUsers(this.entities);
    CommonLayer.User User = dau.GetUser(UserId);
    CommonLayer.Role Role = dar.GetRole(RoleCode);
    dar.AllocateUserRole(User, Role);
}

以下是数据层中GetUserGetRole的代码:

public CommonLayer.User GetUser(Guid UserId)
{
    return this.Entities.Users.SingleOrDefault(p => p.UserId.Equals(UserId));
}

public CommonLayer.Role GetRole(string RoleCode)
{
    return this.Entities.Roles.SingleOrDefault(p => p.RoleId == RoleCode);
}

而且,这里是数据层中的AllocateUserRole

public void AllocateUserRole(CommonLayer.User User, CommonLayer.Role Role)
{
    User.Roles.Add(Role);
    this.Entities.SaveChanges();
}

【问题讨论】:

  • 您应该查看 Microsoft 成员资格提供程序的示例代码。 User 和 Role 的标识符都是 GUID,示例应该为您提供有关实现功能的可用方法的一些指导。您包含的代码不是很清楚或不完整,因此您必须自己付出更多的努力。
  • 那么,GetUser 和 GetRole 能找到您所期望的吗?问题仅在 AllocateUserRole 中吗?
  • AllocateUSerRole 似乎是问题,因为 GetUser 和 GetRole 都从数据库中获取值
  • 我认为你想要一个 UserRole 类,并且 User 应该有一个集合。
  • 没有 SQL Server 2010 - 我们有 2000、2005、2008、2008 R2、2012、2014 和 2016 正在准备中 - 你真的是哪一个 使用?

标签: c# sql-server asp.net-mvc entity-framework asp.net-mvc-5


【解决方案1】:

问题是您将RoleIdRoleCode 进行比较。

您将RoleCode 发送到GetRole 方法:

public void AddUserRole(string RoleCode, Guid UserId)
{
        ...
        CommonLayer.Role Role = dar.GetRole(RoleCode);
        ...
}

但是您的 GetRole 方法将其与 RoleId 进行比较

public CommonLayer.Role GetRole(string RoleCode)
    {
        return this.Entities.Roles.SingleOrDefault(p => p.RoleId == RoleCode);
    }

更新:

我意识到您忘记将用户添加到 DbContext ,在您的上下文中是 this.Entities

public void AllocateUserRole(CommonLayer.User User, CommonLayer.Role Role)
{
        User.Roles.Add(Role); // this does nothing to your Database. 

        //You should use this to add user to your context ( db )            
        this.Entities.Users.Add(User); 

        // or this if you want to update your user in your context ( db ).
        this.Entities.Set<CommonLayer.User>().Attach(User);
        this.Entities.Entry(User).State = EntityState.Modified;

        this.Entities.SaveChanges();
}

【讨论】:

  • 基本上是一样的,因为在数据库中,我将 roleID 保存为 varchar,就像在角色代码中一样,例如用户 - “USR”作为主键
  • @Student 您正在更新对您的数据库没有影响的用户对象。请查看更新后的答案。
猜你喜欢
  • 2011-05-31
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多