C#
public class Product
{
public int Id { get; set; }
public string Name{ get; set; }
// lotsa other stuff
public ICollection RelatedProduct{ get; set; }
}Hi,
Let's say I have a table "Product"
That table has a collection of 'Related Products'
Ho do I configure it on the DbContext fulent api so it creates the link table?
One product can be in many "relatedproducts" and each product can have many "relatedproducts"

Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 5, 2023, 10:55 PM EDT
To create a many-to-many relationship on the same table in SQL using Entity Framework, you can follow these steps:
1. Define the model class for the table. Let's assume you have a table named "Person" with a primary key "Id" and a self-referencing foreign key "FriendId" that represents the many-to-many relationship.
public class Person { public int Id { get; set; } public string Name { get; set; } public ICollection Friends { get; set; } public ICollection FriendOf { get; set; } }2. Configure the many-to-many relationship in your DbContext class by overriding the OnModelCreating method. Use the Fluent API to define the relationship.
public class YourDbContext : DbContext { public DbSet People { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasMany(p => p.Friends) .WithMany(p => p.FriendOf) .UsingEntity(j => j.ToTable("Friendship") .HasData(new { FriendsId = 1, FriendOfId = 2 }, new { FriendsId = 2, FriendOfId = 3 }, new { FriendsId = 3, FriendOfId = 1 })); // Example data, you can customize this base.OnModelCreating(modelBuilder); } }3. Use the Friends and FriendOf properties to navigate the many-to-many relationship.
using (var context = new YourDbContext()) { var person1 = new Person { Name = "Alice" }; var person2 = new Person { Name = "Bob" }; var person3 = new Person { Name = "Charlie" }; person1.Friends = new List { person2 }; person2.Friends = new List { person3 }; person3.Friends = new List { person1 }; context.People.AddRange(person1, person2, person3); context.SaveChanges(); }In this example, we create a many-to-many relationship between people using the Friends and FriendOf properties. We define the relationship in the OnModelCreating method using the HasMany and WithMany methods. The UsingEntity method is used to configure the intermediate table, "Friendship," that stores the relationship data. Finally, we can create and save entities to establish the many-to-many relationship.
Mudzakkir TohaRank #3,303 · 435 reputationPosted Jun 5, 2023, 7:38 PM EDT
You can create tables first. and do scaffold to your project
try to search scaffold on youtube or you can learn here
https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx
Mudzakkir TohaRank #3,303 · 435 reputationPosted Jun 5, 2023, 6:49 PM EDT
There are number ways to achieve this.
In Example you create product tags, which is relation table between product and tag tables.
Andre Silveira154 reputationPosted Jun 5, 2023, 7:14 PM EDT
@Mudzakkir Toha
But how do I do it in EF core? I know how to make it in SQL server, but I need it in entity framework core..