Skip to content

Instantly share code, notes, and snippets.

@Patrick-Ullrich
Created November 25, 2020 02:07
Show Gist options
  • Save Patrick-Ullrich/aee278201c269e09f57e2bb0ae71de71 to your computer and use it in GitHub Desktop.
Save Patrick-Ullrich/aee278201c269e09f57e2bb0ae71de71 to your computer and use it in GitHub Desktop.
DbContext w/ M2M
public class StreamingContext : DbContext
{
public StreamingContext(DbContextOptions<StreamingContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Streamer>()
.HasMany(s => s.Subscribers)
.WithMany(s => s.Streamers)
.UsingEntity<Subscription>(
j => j.HasOne(s => s.Subscriber).WithMany(s => s.Subscriptions),
j => j.HasOne(s => s.Streamer).WithMany(s => s.Subscriptions));
}
public DbSet<Streamer> Streamers { get; set; }
public DbSet<Subscriber> Subscribers { get; set; }
public DbSet<Subscription> Subscriptions { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment