在实体框架中映射联接表

发布于 2024-12-27 09:30:33 字数 1371 浏览 1 评论 0原文

我正在尝试将 asp.net 成员资格表连接到 asp.mvc 3 网站。我一直在遵循 Steve Sanderson 的《Pro ASP.NET MVC 3 Framework》一书中针对体育商店的教程,并将其应用到从会员 exe 生成的表中。

所以我有一个如下所示的用户类:

namespace Domain.Entities
{
    public class User
    {
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public DateTime LastActivityDate;
        public virtual ICollection<Role> Roles { get; set; }
    }

    public class Role
    {
        public Guid RoleId { get; set; }
        public string RoleName { get; set; }
    }
}

和一个如下所示的上下文类:

public class EFDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().ToTable("aspnet_users");
        modelBuilder.Entity<Role>().ToTable("aspnet_roles");
    }
}

但我收到错误,因为我假设它正在寻找这两个表之间的联接,而实际上存在一个联接表(aspnet_UsersInRoles) 之间以避免多对多链接,当我尝试从用户引用角色模型时,例如:

var test = _repository.Users.FirstOrDefault().Roles.Count();

{"列名“User_UserId”无效。\r\n列名无效 “User_UserId”。\r\n列名“User_UserId”无效。"}

有没有办法使用实体框架使用联接表将表映射到一起?最好只是添加一个新的 ADO.NET 实体数据模型并让Visual Studio 只是对数据库进行逆向工程?

I am trying connect the asp.net membership tables in to an asp.mvc 3 website. I have been following the tutorial in Steve Sanderson's book 'Pro ASP.NET MVC 3 Framework' for the sport store and applying this to the tables that are generated from the membership exe.

So I have a user class that looks like:

namespace Domain.Entities
{
    public class User
    {
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public DateTime LastActivityDate;
        public virtual ICollection<Role> Roles { get; set; }
    }

    public class Role
    {
        public Guid RoleId { get; set; }
        public string RoleName { get; set; }
    }
}

and a context class that looks like:

public class EFDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().ToTable("aspnet_users");
        modelBuilder.Entity<Role>().ToTable("aspnet_roles");
    }
}

but I get an error because I assume it's looking for a join between these two tables when in fact there is a join table (aspnet_UsersInRoles) in between to avoid a many to many link, when I try and reference the Role model from the User, such as:

var test = _repository.Users.FirstOrDefault().Roles.Count();

{"Invalid column name 'User_UserId'.\r\nInvalid column name
'User_UserId'.\r\nInvalid column name 'User_UserId'."}

is there a way to map the table together using the join table using the entity framework? Is it better just to add a new ADO.NET entity data model and let visual studio just reverse engineer the database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

時窥 2025-01-03 09:30:33

您需要自定义多对多映射,如下所示

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
       modelBuilder.Entity<User>().ToTable("aspnet_users");
       modelBuilder.Entity<Role>().ToTable("aspnet_roles");

       modelBuilder.Entity<User>()
       .HasMany(u => u.Roles).WithMany(r => r.Users)
            .Map(m =>
            {
                m.ToTable("aspnet_UsersInRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });
   }

编辑:您还需要将Users属性添加到Role类。否则映射应更改为

       modelBuilder.Entity<User>()
       .HasMany(u => u.Roles).WithMany()
            .Map(m =>
            {
                m.ToTable("aspnet_UsersInRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });

You need to customize the many to many mapping as follows

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
       modelBuilder.Entity<User>().ToTable("aspnet_users");
       modelBuilder.Entity<Role>().ToTable("aspnet_roles");

       modelBuilder.Entity<User>()
       .HasMany(u => u.Roles).WithMany(r => r.Users)
            .Map(m =>
            {
                m.ToTable("aspnet_UsersInRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });
   }

Edit: You would also need to add Users property to the Role class. Otherwise the mapping should be changed to

       modelBuilder.Entity<User>()
       .HasMany(u => u.Roles).WithMany()
            .Map(m =>
            {
                m.ToTable("aspnet_UsersInRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文