EF - Basic Query 1

CSharp · Console

/Program.cs

// @nuget: EntityFramework

using System;
using System.Data.Entity;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

public class Program
{
	public static void Main()
	{
		InsertData();
		
		using (var context = new BookStore())
		{			
			var books = context.Books.ToList();
			
			FiddleHelper.WriteTable(books); 
		}
	}
	
	public static void InsertData()
	{
		using (var context = new BookStore())
		{
			Book book1 = new Book() { Title = "Fundamentals of Computer Programming with C#"};
			Book book2 = new Book() { Title = "Java: A Beginner's Guide"};
			Book book3 = new Book() { Title = "Learn VB.NET"};
			Book book4 = new Book() { Title = "C# Fundamentals for Absolute Beginners"};
			Book book5 = new Book() { Title = "SQL: The Ultimate Beginners Guide"};
				
			context.Books.Add(book1);
			context.Books.Add(book2);
			context.Books.Add(book3);
			context.Books.Add(book4);
			context.Books.Add(book5);
		
		    context.SaveChanges(); 
		}
	}
	
	public class BookStore : DbContext
    {
        public BookStore() : base(FiddleHelper.GetConnectionStringSqlServer())
        {
            
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }

        public DbSet<Book> Books { get; set; }
    }
	
	public class Book
    {
        public int BookId { get; set; }
        public string Title { get; set; }
    }
}
;