Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:41:43+00:00 2026-06-06T23:41:43+00:00

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

  • 0

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

There is a Users table that has the following columns:

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

User configuration:

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

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

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

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

1 7 8
2 7 9

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

Just for clarity if you are still confused…

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:41:45+00:00Added an answer on June 6, 2026 at 11:41 pm

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

    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()
    

    This will give an IEnumerable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Entity Framework 4.1 code first to connect to an already existing
I am using Entity Framework Code First to create a database table. My model
I'm using Entity Framework 4.3 Code First with a custom database initializer like this:
I am creating a new database model using Entity Framework 4.3 Code-FIrst; using Fluent
I am using Entity Framework Code First. I want to query entites from database
I'm creating a database using entity framework code first and I'm having some issues
I am successfully creating database (SQL Ce) using Entity Framework Code First approach (C#-WPF).
Using the Entity Framework Code First paradigm I have defined the following objects and
I'm trying to create a billing database with Entity Framework 4.3 using Code First
I'm using Entity Framework code first and pulling some data back from our database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.