I'm not using code first, I created a table called artists with an ID and name field, a table called albums with an id and title field and a junction_artists_albums table with artist_id and album_id.
Here are my model/context classes:
Artist:
public class Artist
{
public int Id { get; set; }
public string ArtistName { get; set; }
public ICollection<Album> albums { get; set; }
}
Album:
public class Album
{
public int Id { get; set; }
public string AlbumName { get; set; }
public ICollection<Artist> artists { get; set; }
}
MusicDBContext:
public class MusicDBContext : DbContext
{
public MusicDBContext() { }
public DbSet<Artist> Artists { get; set; }
public DbSet<Album> Albums { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Album>().HasMany<Artist>(a => a.artists).WithMany(a => a.albums);
}
}
This doesn't work because the junction table isn't being filled in. Excuse me if this is a stupid question but I'm very new to Entity Framework