1

Trying to create a class that I can connect 2 applicationusers together, but migration is adding extra userid into the mix making me think im doing this wrong,

public class UserReview
{
    [Key]
    [Column(Order = 5)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Column(Order = 10)]
    [ForeignKey("ApplicationUserReviewer")]
    public string ReviewerUserId { get; set; }
    public virtual ApplicationUser ApplicationUserReviewer { get; set; }

    [Column(Order = 15)]
    [ForeignKey("ApplicationUserReviewed")]
    public string ReviewedUserId { get; set; }
    public virtual ApplicationUser ApplicationUserReviewed { get; set; } 
/*few more fields*/

}

In the application user i've added

public virtual ICollection<UserReview> UserReviews { get; set; }

In ApplicationDbContext added

public DbSet<UserReview> UserReviews { get; set; }

Migration generates something in this sense with extra userid at the end

ID = c.Int(nullable: false, identity: true),
                        ReviewerUserId = c.String(maxLength: 128),
                        ReviewedUserId = c.String(maxLength: 128),
/*....*/
                        ApplicationUser_Id = c.String(maxLength: 128),

any suggestions to get around this,

Also I tried just UserId and applicationuser_id for the first userid still tries to add the 3rd reference.

drewex
  • 317
  • 3
  • 13
  • So you are creating 2 connections between UserReview and ApplicationUser? Then when you create this collection of UserReviews EF probably don't know which UserReviews you try to map, the ones which user Created or ones that was created to review this User – Jacob Sobus May 10 '16 at 07:57
  • Look at this one: http://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table the same problem, you try to map twice and create only one collection on User. Add Configuration for this mapping like in link above. – Jacob Sobus May 10 '16 at 08:02
  • 1
    Thx Jacob that did it. Wish you answered it with that, so i can give you the thumbs up. What search did you use to find this, I spent some time trying to find that same info. – drewex May 10 '16 at 16:40
  • Got a problem with this .Include(u => u.....) doesnt work like it used to with other tables. I am missing something but what. – drewex May 10 '16 at 18:34
  • I just got problem like this before :) It's mapping more than one collection from same table. Added answer for historical use. – Jacob Sobus May 10 '16 at 19:25

1 Answers1

4

The problem is that you have 2 connections between User and UserReview, so when you create that collection of UserReviews Entity Framework don't really know which connection you would like to use. To configure such thing you should configure it with FluentAPI on your DBContext class. Look at this answer Entity Framework Code First - two Foreign Keys from same table

Do something like this:

modelBuilder.Entity<UserReview>()
                .HasRequired(m => m.ApplicationUserReviewer)
                .WithMany(t => t.UserReviews)
                .HasForeignKey(m => m.ApplicationUserReviewerId);
Community
  • 1
  • 1
Jacob Sobus
  • 961
  • 16
  • 25