27

I have a many to many relationship and I want to store extra data in the couple-table, using Code First Fluent API.

How can this be achieved ?

My model:

A user can have 1 or more badges (optional), a badge can belong to one or more users (optional). I want to store an extra field (called B) for this relation to be stored. The table should be named UserBadges with the following fields: UserId, BadgeId, B

(I have seen this earlier in StackOverflow here, but I the model is somewhat complex and no answer has been given correctly yet)

1 Answer 1

47

You cannot map it as many-to-many directly. If you add additional field to the junction table and you want to access that field in the application you need to promote your junction table to entity instead and use two one-to-many relations:

public class Badge {
    ...
    public virtual ICollection<UserBadge> UserBadges { get; set; }
}

public class User {
    ...
    public virtual ICollection<UserBadge> UserBadges { get; set; }
}

public class UserBadge {
    public int UserId { get; set; }
    public int BadgeId { get; set; }
    public string B { get; set; }
    public virtual Badge Badge { get; set; }
    public virtual User User { get; set; }
}

The default conventions should define the mapping correctly except the key for UserBadge table which must be done either in Fluent-API or data annotations.

modelBuilder.Entity<UserBadge>().HasKey(e => new { e.UserId, e.BadgeId });
12
  • 1
    Thx, but I doubt if this works, because you define UserId and BadgeId as a compound PK. It should be possible to have 2 records with the same UserId and BadgeId in it, the B value does make the distinct value of it (it is a FK to another table). Mar 1, 2013 at 20:30
  • 3
    I know this is really old, but isn't the collections in UserBadge wrong? A single UserBadge should only have a single Badge and a single User?
    – NiklasJ
    Aug 22, 2014 at 9:28
  • 2
    "which must be done either in Fluent-API or data annotations." Could you share how to do that by annotations, please?
    – Yoda
    May 30, 2015 at 16:38
  • 1
    Please excuse my ignorance, but does that mean that I now have to maintain another (join) entity in the code? The last time I worked with EF 5.0, EF was able to handle everything using Navigation Properties for the M:N scenario.
    – Vaibhav
    Aug 6, 2016 at 12:47
  • 2
    @Vaibhav: EF is able to handle everything using navigation properties for the M:N scenario if your join table contains only keys for related tables. If you need any more records you need a separate join entity to have access to them. Aug 9, 2016 at 11:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.