Entity Framework Custom Roles from Many to Many

Hi, I am trying to customize my entity structure for the relationship of many, many users between a user and a role.

The following figure shows what is in the database:

Many to many relationships

User Model:

public class User : IEntity
    {
        public virtual int UserId { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(100)]
        public virtual string UserName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(100)]
        public virtual string FirstName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(100)]
        public virtual string LastName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(200)]
        public virtual string EmailAddress { get; set; }
        public int AreaId { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string CreatedByUserName { get; set; }
        public DateTime CreatedDateTime { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string LastModifiedByUserName { get; set; }
        public DateTime? LastModifiedDateTime { get; set; }

        //Navigation properties
        //public virtual Role Role { get; set; }
        public virtual Area Area { get; set; }

        public virtual ICollection<Role> Roles { get; set; }

}

Model for the role:

public class Role : IEntity
    {
        public int RoleId { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(100)]
        public string Name { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(1000)]
        public string Description { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string CreatedByUserName { get; set; }
        public DateTime CreatedDateTime { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string LastModifiedByUserName { get; set; }
        public DateTime? LastModifiedDateTime { get; set; }

        //Navigation Properties
        public ICollection<User> Users { get; set; }
    }

UserRole:

public class UserRole
    {
        public int UserId { get; set; }
        public int RoleId { get; set; }

        //Navigation properties
        public virtual User User { get; set; }
        public virtual Role Role { get; set; }
    }

So, I thought I had it configured fine, but in my code I go something like this:

var roles = from r in user.Roles
                        select r.Name;

and he shoots himself, causing errors:

Server Error in '/' Application.
Invalid object name 'dbo.RoleUser'. 

so I added the following to the context:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .HasMany(i => i.Roles)
                .WithMany(u => u.Users);

}

However, now I get errors:

Server error in application "/". Invalid column name 'Role_RoleId'. Invalid column name 'User_UserId'.

So, I have nothing right. Can someone point me in the right direction?

+5
source share
1

UserRole , . UserRole.

, EF RoleUser. , .

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
        modelBuilder.Entity<User>()
            .HasMany(i => i.Roles)
            .WithMany(u => u.Users)
            .Map(m =>
            {
                m.ToTable("UserRole");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });

 }
+9

All Articles