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 7715755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:28:01+00:00 2026-06-01T02:28:01+00:00

I need to map a many-to-many relationship using Entity Framework Code First. Its a

  • 0

I need to map a many-to-many relationship using Entity Framework Code First. Its a standard socialnetworking FriendRequests mapping. A User Object has a Collection of FriendRequests of type List<User>. In the database I’m using a join table for the relationship as follows:

CREATE TABLE dbo.FriendRequests(
  UserId INT NOT NULL FOREIGN KEY REFERENCES dbo.Users(id),
  FriendId INT NOT NULL FOREIGN KEY REFERENCES dbo.Users(id),
  RequestDate SMALLDATETIME NOT NULL DEFAULT GETUTCDATE())
GO
ALTER TABLE dbo.FriendRequests ADD PRIMARY KEY (UserId,FriendId) 
GO

How do I map the user object in Entity Framework Code First to enable a Collection via a join table?

  • 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-01T02:28:02+00:00Added an answer on June 1, 2026 at 2:28 am

    You can try it this way:

    public class User
    {
        public int Id { get; set; }
    
        public ICollection<FriendRequest> FriendRequests { get; set; }
    }
    
    public class FriendRequest
    {
        public int UserId { get; set; }
        public int FriendId { get; set; }
    
        public DateTime RequestDate { get; set; }
    
        public User User { get; set; }
        public User Friend { get; set; }
    }
    

    Mapping with Fluent API:

    modelBuilder.Entity<User>()
        .HasMany(u => u.FriendRequests)
        .WithRequired(f => f.User)
        .HasForeignKey(f => f.UserId);
    
    modelBuilder.Entity<FriendRequest>()
        .HasKey(f => new { f.UserId, f.FriendId });
    
    modelBuilder.Entity<FriendRequest>()
        .HasRequired(f => f.Friend)
        .WithMany()
        .HasForeignKey(f => f.FriendId);
    

    Because of the RequestDate property in the link table you cannot map this as a many-to-many relationship. Instead you need two one-to-many relationships as shown above.

    Possibly you need to disable cascading delete, I am not sure. You can do this by appending .WillCascadeOnDelete(false) at the end of the two one-to-many mappings.

    Edit

    To your comment: If you remove the RequestDate column you can create your model with a many-to-many relationship. You don’t need the FriendRequest entity in this case:

    public class User
    {
        public int Id { get; set; }
    
        public ICollection<User> FriendRequests { get; set; }
    }
    

    Mapping with Fluent API:

    modelBuilder.Entity<User>()
        .HasMany(u => u.FriendRequests)
        .WithMany()
        .Map(m =>
        {
            m.ToTable("FriendRequests");
            m.MapLeftKey("UserId");
            m.MapRightKey("FriendId");
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Entity Framework 4.1 code-first and I'm having a slight issue with retriving
I'm using python and I need to map locations like Bloomington, IN to GPS
In php, I often need to map a variable using an array ... but
Can I somehow tell NHibernate to map my one-to-many relationship to a property which
I have a Hash Map (many-to-one relationship between texts and boolean values): name flag
I need to use map in my Android application. There are many Android platforms.
I am developing an app in which i need to use map kit(first time)
I need to map most of the computer memory as uswc to take advantage
I need to map a class which has a list of Enums to a
I have a simple requirement, i need a map of type . however i

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.