0

我得到了具有自定义MembershipProvider的MVC 3应用程序,因此它将新注册的用户存储到我的数据库中。 我对现有数据库使用Code First方法(我为此使用了实体框架电源工具)。有表用户,角色和用户角色。 在用户表我: 用户ID(PK) 用户名密码 电子邮件 ... 实体框架(代码优先),多对多,用户和角色

在表角色我 角色ID(PK) 名称 说明

在表UsersRoles我 用户ID(设为复合PK) 角色ID(设为复合PK)

public partial class User 
{ 
    public User() 
    { 
     this.Roles = new List<Role>(); 
    } 

    public int UserID { get; set; } 
    public string Username { get; set; } 
    public string Password { get; set; } 
    public string Email { get; set; } 
    public virtual ICollection<Role> Roles { get; set; } 
} 

public partial class Role 
{ 
    public Role() 
    { 
     this.Users = new List<User>(); 
    } 
    public int RoleID { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public virtual ICollection<User> Users { get; set; } 
} 


public class UserMap : EntityTypeConfiguration<User> 
{ 
    public UserMap() 
    { 
     // Primary Key 
     this.HasKey(t => t.UserID); 

     // Properties 
     this.Property(t => t.Username) 
      .IsRequired() 
      .HasMaxLength(20); 

     this.Property(t => t.Password) 
      .IsRequired() 
      .HasMaxLength(50); 

     this.Property(t => t.Email) 
      .IsRequired() 
      .HasMaxLength(100); 



     // Table & Column Mappings 
     this.ToTable("Users"); 
     this.Property(t => t.UserID).HasColumnName("UserID"); 
     this.Property(t => t.Username).HasColumnName("Username"); 
     this.Property(t => t.Password).HasColumnName("Password"); 
     this.Property(t => t.Email).HasColumnName("Email"); 

    } 
} 

public class RoleMap : EntityTypeConfiguration<Role> 
{ 
    public RoleMap() 
    { 
     // Primary Key 
     this.HasKey(t => t.RoleID); 

     // Properties 
     this.Property(t => t.Name) 
      .IsRequired() 
      .HasMaxLength(20); 

     this.Property(t => t.Description) 
      .HasMaxLength(50); 

     // Table & Column Mappings 
     this.ToTable("Roles"); 
     this.Property(t => t.RoleID).HasColumnName("RoleID"); 
     this.Property(t => t.Name).HasColumnName("Name"); 
     this.Property(t => t.Description).HasColumnName("Description"); 

     // Relationships 
     this.HasMany(t => t.Users) 
      .WithMany(t => t.Roles) 
      .Map(m => 
       { 
        m.ToTable("UsersRoles"); 
        m.MapLeftKey("RoleID"); 
        m.MapRightKey("UserID"); 
       }); 


    } 
} 

我在角色表中有预定义的角色,我不希望在新用户注册时修改它们(角色表只能由管理员修改),我希望将新用户存储在用户表中,并为其分配默认角色普通用户(没有特殊权限),因此它向Users表添加一条记录,在UsersRoles表中添加一条记录。

public void AddUser(User user) 
    { 
     Users.Add(user); 
     SaveChanges(); 
    } 

var tmp = new User { Username = username, Password = GetMd5Hash(password), Email = email }; 


      using (var db = new mycontext()) 
      { 
       mycontext.AddUser(userObj); 
      } 

但我不知道如何使它添加一个新的记录UsersRoles table..with用户ID和角色ID(默认的,第一个角色ID = 1 - 普通用户权限)。任何帮助将不胜感激。

+0

如何不添加任何角色并假定没有任何角色的用户是普通用户? – Oybek 2013-05-04 19:20:57

回答

0

你只是从数据库读取所需的角色和你做

role.Users.Add(user); 
SaveChanges(); 

EF将创建联接表的新纪录。请注意,Users集合必须在此处加载。

这样做,您甚至不需要保存用户的语句Users.Add(user);

同样,您可以将角色对象添加到user.Roles

当你想在接线表中删除记录删除从作用加载user.Roles集合或role.Users用户。

+0

谢谢,帮助。 :)你能指出我在哪里我可以找到一些有用的信息或关于修改成员资格的例子,所以它可以与我的角色表一起使用。因此,我可以使用_ [授权(角色=“管理员,超级用户”)] _有效。先谢谢你。 – oxfox 2013-05-05 11:37:53