如何处理关联表连接相同的两个表 [英] How to handle an association table linking the same 2 tables

查看:396
本文介绍了如何处理关联表连接相同的两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架4.3 code首先使用已经存在的数据库。

I am using Entity Framework 4.3 code first using an already existing database.

有一个用户表具有以下列:

- UserID (int)
- FirstName (string)
- Surname (string)

我有一个名为扮演的关联表。该扮演表是用户表和相同的用户台之间的关联表。用户可以具有的用户的列表。这里是模仿秀表结构:

I have an association table called Impersonations. The Impersonations table is an association table between a user table and the same user table. A user can have a list of users. Here is the Impersonations table structure:

- ImpersonateID (int primary key)
- UserID (int FK to Users table's UserID column)
- ImpersonatedUserID (int FK to Users table's UserID column)

我有,我想映射到这些列属性的用户等级:

I have a User class with properties that I want mapped to these columns:

public class User : IEntity
{
     public int Id { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public string EmployeeNumber { get; set; }
     public virtual ICollection<User> ImpersonatedUsers { get; set; }
     public virtual ICollection<User> Users { get; set; }
}

在我的数据库上下文类我有以下几点:

In my db context class I have the following:

public DbSet<User> Users { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Configurations.Add(new UserConfiguration());
}

用户配置:

class UserConfiguration : EntityTypeConfiguration<User>
{
     internal UserConfiguration()
     {
          this.Property(x => x.Id).HasColumnName("UserID");
          this.Property(x => x.LastName).HasColumnName("Surname");
          this.Property(x => x.EmployeeNumber).HasColumnName("StaffNumber");

          this.HasMany(i => i.Users)
               .WithMany(c => c.ImpersonatedUsers)
               .Map(mc =>
               {
                    mc.MapLeftKey("UserID");
                    mc.MapRightKey("ImpersonatedUserID");
                    mc.ToTable("Impersonations");
               });
     }
}

我有没有这个链接正确使用实体框架?如果我通过一个用户ID,然后在需要返回被标记为inpersonated用户的用户的列表。我该怎么办呢?我有这个,但无论用户和ImpersonatedUsers都是空的:

Did I link this up correctly using Entity Framework? If I pass through a user id then a list of users needs to be returned that was marked as inpersonated users. How I do this? I have this but both User and ImpersonatedUsers are empty:

return DbContext.Users
     .Include("ImpersonatedUsers")
     .SingleOrDefault(x => x.Id == userId);

可以说,我有我的用户表如下数据(ID,姓,名,员工数量):

Lets say I have the following data in my Users table (id, first name, last name, employee number):

7 Craig Matthews 123456
8 Susan Renshaw 234567
9 Michelle du Toit 34567

和我扮演的表如下数据(模拟ID,用户ID,模拟的用户ID):

And the following data in my Impersonations table (impersonated id, user id, impersonated user id):

1 7 8
2 7 9

所以,如果我通过以7:3的用户ID则需要返回记录用户苏珊伦肖和米歇尔都伊特。

So if I pass in a user id of 7 then it need to return records for users Susan Renshaw and Michelle du Toit.

只是为了清楚起见,如果你还在迷茫......

我也有类似的情况。我有一个产品表和规格表。这2个是由assication表称为ProductSpecifications联系起来。我想在这里实现相同,但2表将是用户表和关联表的模仿秀。

I have a similar situation. I have a Products table and a Specifications table. These 2 are linked up by the assication table called ProductSpecifications. I am trying to achieve the same here, but the 2 tables would be the Users table and the association table is Impersonations.

推荐答案

如果我很明白,我想我有完全一样的:一个用户也是一组,因此可以拥有成员

If I well understand, I think I have quite the same: a user is also a group and so can have members.

下面是我的code摘录:

Here is an extract of my code:

public partial class User : AuthorizableBaseObject, IHasUID {
    public User() {
        Members = new List<User>();
    }

    public Guid UID { get; set; }
    public DateTime EmailValidationDate { get; set; }

    public String EMail { get; set; }

    //public Int64 Id { get; set; }
    public String Login { get; set; }
    public String Password { get; set; }
    public String Description { get; set; }
    public DateTime LastConnectionDate { get; set; }

    public Boolean CanConnect { get; set; }
    public Boolean IsDisabled { get; set; }

    public virtual List<User> Members { get; set; }
}

,配置如下:

with the following configuration :

public class UserConfiguration : EntityTypeConfiguration<VDE.User> {
    public UserConfiguration()
        : base() {
        ToTable("Users", "dbo");

        Property(u => u.EMail).IsRequired().HasColumnType("nvarchar").HasMaxLength(100);
        Property(u => u.Login).IsRequired().HasColumnType("nvarchar").HasMaxLength(20);
        Property(u => u.Password).IsRequired().HasColumnType("nvarchar").HasMaxLength(50);
        Property(u => u.Description).HasColumnType("nvarchar").HasMaxLength(200);
        Property(u => u.LastConnectionDate).HasColumnType("datetime2");

        HasMany(u => u.Members).WithMany().Map(m => m.MapLeftKey("UserId").MapRightKey("MemberId").ToTable("UserMembers"));
    }
}

从这里得到一组的成员查询很简单:

From here to get the members of a group the query is easy:

context.Users.Where(u => u.Id == someId).Select(u => u.Members).FirstOrDefault()

这会给一个IEnumerable

This will give an IEnumerable

这篇关于如何处理关联表连接相同的两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆