簡體   English   中英

實體框架(代碼優先),多對多,用戶和角色

[英]Entity framework (code first), many to many, users and roles

我使用自定義MembershipProvider獲得了MVC 3應用程序,因此它將新注冊的用戶存儲到我的數據庫中。 我在現有數據庫中使用了代碼優先方法(為此,我使用了實體框架強大的工具)。 得到了表Users,Roles和UsersRoles。 在我獲得的表Users中:UserID(PK)用戶名密碼電子郵件...

在“角色”表中,我獲得了RoleID(PK)名稱描述

在表UsersRoles中,我獲得了UserID(設置為復合PK)RoleID(設置為復合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);
            }

但是我不知道如何使它添加到具有UserID和RoleID(默認值,第一個RoleID = 1-普通用戶權限)的UsersRoles表中的新記錄。 任何幫助將不勝感激。

您只需從數據庫中獲取所需的角色,然后執行

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

EF將在聯結表中創建一個新記錄。 請注意,此時必須加載Users集合。

這樣做,您甚至不需要語句Users.Add(user); 保存用戶。

同樣,您可以向user.Roles添加角色對象。

當您要刪除聯結表中的記錄時,請從已加載的 user.Roles集合中刪除一個角色,或者從role.Users刪除一個用戶。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM