using System.Data.Entity;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
public static void Main()
using (var context = new BookStore())
var authors = context.Authors
.Where(a => a.Books.Any()).ToList();
public static void DisplayData(List<Author> list)
foreach(var author in list)
Console.WriteLine(author.FirstName + " " + author.FirstName);
foreach(var book in author.Books)
Console.WriteLine("\t" + book.Title);
public static void InsertData()
using (var context = new BookStore())
var Authors = new List<Author>
new Author() {FirstName = "Mark", LastName = "Cuban"},
new Author() {FirstName = "Andy", LastName = "Holloway"},
new Author() {FirstName = "Johny", LastName = "bravo"}
var Books = new List<Book>
new Book() { Title = "Fundamentals of Computer Programming with C#", AuthorId = 1},
new Book() { Title = "Java: A Beginner's Guide", AuthorId = 1},
new Book() { Title = "SQL: The Ultimate Beginners Guide", AuthorId = 2},
new Book() { Title = "Learn VB.NET", AuthorId = 2},
new Book() { Title = "C# Fundamentals for Absolute Beginners", AuthorId = 2}
context.Authors.AddRange(Authors);
context.Books.AddRange(Books);
public class BookStore : DbContext
public BookStore() : base(FiddleHelper.GetConnectionStringSqlServer())
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
public int BookId { get; set; }
public string Title { get; set; }
public int AuthorId { get; set; }
public Author Author { get; set; }
public int AuthorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Book> Books { get; set; }