Many-to-Many Relationships to Same Table with EF

paul simmons

I have a table where I need many to many relationships, an example can be a social network where friendships are kept. e.g.:

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }
}

corresponding to table

CREATE TABLE Users (
UserId int,
UserName varchar(255)
);

I need to have a table like:

CREATE TABLE Friendships(
UserId1 int,
UserId2 int
);

where both fields are related to Users.UserId. How can I achieve this with annotations in EF?

Lars Anundskås

Something like this would do it

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }

    public ICollection<User> FriendShips { get; set; }
    public User()
    {
        FriendShips = new HashSet<User>();
    }
}


public class SocialContext : DbContext
{
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>()
        .HasMany(p => p.FriendShips)
        .WithMany()
        .Map(mc =>
        {

            mc.ToTable("userfriends");
        });
    }
}

A single list for each user will keep each users list of friends

Test:

    using (SocialContext ctx = new SocialContext())
    {
        User u = new User()
        {
            UserName = "John"
        };

        User uu = new User()
        {
            UserName = "Jack"
        };

        uu.FriendShips.Add(u);
        u.FriendShips.Add(uu);

        ctx.Users.Add(u);

        ctx.SaveChanges();
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Two One-to-Many relationships in the same table

From Dev

Adding multiple one to many relationships to one table using EF

From Dev

EF 6.0 Many to Many Relationships edits

From Dev

Reference many-to-many relationships by ID with EF

From Dev

DeleteRule for Many-to-many relationships ef codefirst

From Dev

Many-to-many relationship between same table with extra column in EF

From Dev

How to define EF Many To Many Relationship in type that has other relationships to the same other type

From Dev

Two many-to-many relationships for one table

From Dev

One table or many tables with relationships

From Dev

Many to many relationship in the same table?

From Dev

How to operate on the table many to many EF

From Dev

How to operate on the table many to many EF

From Dev

Issue with many to many relationships

From Dev

Defining a many-to-many relationship referencing the same table (EF7/core)

From Dev

Multiple One-to-Many Relationships to the Same Entity

From Dev

EF 7 beta 6 : Entities with one to many relationships in EF 7

From Dev

Entity Splitting For One-To-Many table relationships

From Dev

How to change the naming convention of Many-to-Many table relationships?

From Dev

Many to Many Constraint to same parent table

From Dev

Entity Framework many to many on same table

From Dev

EF Code First: Entity type with multiple many-to-one relationships

From Dev

EF LEFT OUTER JOIN instead of INNER JOIN in one to many relationships

From Dev

EF Code First: Entity type with multiple many-to-one relationships

From Dev

Ef7 One To many relationships HasOne WithMany and Attatch

From Dev

Many-to-many hierarchical relationships

From Dev

Many to many relationships MongoDB mongoose

From Dev

Simple many-to-many relationships

From Dev

SQL Many-To-Many relationships

From Dev

Ember Data - Many to Many Relationships?

Related Related

  1. 1

    Two One-to-Many relationships in the same table

  2. 2

    Adding multiple one to many relationships to one table using EF

  3. 3

    EF 6.0 Many to Many Relationships edits

  4. 4

    Reference many-to-many relationships by ID with EF

  5. 5

    DeleteRule for Many-to-many relationships ef codefirst

  6. 6

    Many-to-many relationship between same table with extra column in EF

  7. 7

    How to define EF Many To Many Relationship in type that has other relationships to the same other type

  8. 8

    Two many-to-many relationships for one table

  9. 9

    One table or many tables with relationships

  10. 10

    Many to many relationship in the same table?

  11. 11

    How to operate on the table many to many EF

  12. 12

    How to operate on the table many to many EF

  13. 13

    Issue with many to many relationships

  14. 14

    Defining a many-to-many relationship referencing the same table (EF7/core)

  15. 15

    Multiple One-to-Many Relationships to the Same Entity

  16. 16

    EF 7 beta 6 : Entities with one to many relationships in EF 7

  17. 17

    Entity Splitting For One-To-Many table relationships

  18. 18

    How to change the naming convention of Many-to-Many table relationships?

  19. 19

    Many to Many Constraint to same parent table

  20. 20

    Entity Framework many to many on same table

  21. 21

    EF Code First: Entity type with multiple many-to-one relationships

  22. 22

    EF LEFT OUTER JOIN instead of INNER JOIN in one to many relationships

  23. 23

    EF Code First: Entity type with multiple many-to-one relationships

  24. 24

    Ef7 One To many relationships HasOne WithMany and Attatch

  25. 25

    Many-to-many hierarchical relationships

  26. 26

    Many to many relationships MongoDB mongoose

  27. 27

    Simple many-to-many relationships

  28. 28

    SQL Many-To-Many relationships

  29. 29

    Ember Data - Many to Many Relationships?

HotTag

Archive