CopyPastor

Detecting plagiarism made easy.

Score: 0.7643332481384277; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2018-08-16
by TanvirArjel

Original Post

Original - Posted on 2016-09-29
by smoksnes



            
Present in both answers; Present only in the new answer; Present only in the old answer;

**May be I am late here but @smokesnes, answer will not work for `many-to-many` relationship with same entity in Entity Framework Core version >= 2.1**
But his answer will work fine for the many to many relationship with different entity.
**Here is the way of configuring `many-to-many` relationship with same entity in EF Core >= 2.1 **
public class UserContacts { public int UserId { get; set; } public virtual User User { get; set; } public int ContactId { get; set; } public virtual User Contact { get; set; } } public class User : DomainModel { public List<UserContacts> UserContacts { get; set; } public List<UserContacts> ContactUsers { get; set; } }

**Then in the model builder:**
protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<UserContacts>().HasKey(r => new {r.UserId, r.ContactId});
modelBuilder.Entity<UserContacts>() .HasOne(pt => pt.User) .WithMany(p => p.UserContacts) .HasForeignKey(pt => pt.UserId).OnDelete(DeleteBehavior.Restrict);;
modelBuilder.Entity<UserContacts>() .HasOne(pt => pt.Contact) .WithMany(t => t.ContactUsers) .HasForeignKey(pt => pt.ContactId).OnDelete(DeleteBehavior.Restrict);; }
> So why does this happen? Is there anything I did wrong to map this > many-to-many relationship?
No, you didn't do anything wrong. [It's just not supported][1]. Current status [here][2].
> Many-to-many relationships without an entity class to represent the > join table are not yet supported. However, you can represent a > many-to-many relationship by including an entity class for the join > table and mapping two separate one-to-many relationships.
With EF-Core you should create the entity for the mapping table. Such as `UserContacts`. A complete example in the [docs][1], as mentioned in the comments. I haven't actually tested the code below, but it should look something like this:
public class UserContacts { public int UserId { get; set; } public virtual User User { get; set; }
public int ContactId { get; set; } // In lack of better name. public virtual User Contact { get; set; } }
public class User : DomainModel { public List<UserContacts> Contacts { get; set; } }

And your `modelBuilder`.
modelBuilder.Entity<UserContacts>() .HasOne(pt => pt.Contact) .WithMany(p => p.Contacts) .HasForeignKey(pt => pt.ContactId); modelBuilder.Entity<UserContacts>() .HasOne(pt => pt.User) .WithMany(t => t.Contacts) .HasForeignKey(pt => pt.UserId);

[1]: https://docs.efproject.net/en/latest/modeling/relationships.html#many-to-many [2]: https://github.com/aspnet/EntityFramework/issues/1368

        
Present in both answers; Present only in the new answer; Present only in the old answer;