实体框架用户角色多对多关系

时间:2011-11-08 01:49:03

标签: entity-framework

您好我正在尝试为用户和角色之间的多对多关系设置我的实体框架。

下图显示了数据库中的内容:

Many to Many relationships

用户模型是:

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; }

}

角色模型是:

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; }
    }

所以我认为我的设置很好但是在我的代码中我会像:

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

它射出了自己的错误:

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

所以我在上下文中添加了以下内容:

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

}

但是现在我遇到了错误:

'/'应用程序中的服务器错误。 列名称“Role_RoleId”无效。 列名称“User_UserId”无效。

所以我肯定没有正确设置这里的东西。安妮可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:9)

您不需要将链接表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");
            });

 }