CSharp · Console
// @nuget: EntityFramework
using System;
using System.Data.Entity;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
public class Program
{
public static void Main()
{
InsertData();
Category programmingCat;
using(var context = new BookStore())
{
DisplayData(context.Authors.ToList());
programmingCat = context.Categories
.Where(c => c.CategoryId == 2)
.FirstOrDefault();
}
Book book = new Book()
{
Title = "iOS Fundamentals",
Author = new Author()
{
AuthorId = 1,
Name = "Mark"
},
Categories = new List<Category>()
{
programmingCat,
new Category() { CategoryName = "iOS"},
new Category() { CategoryName = "Swift"}
}
};
InsertOrUpdate(book);
using(var context = new BookStore())
{
Console.WriteLine("\n\n\nAfter Entity Graph\n\n\n");
DisplayData(context.Authors.ToList());
}
}
public static void InsertOrUpdate(Book book)
{
using(var context = new BookStore())
{
context.Entry(book).State = book.BookId == 0 ?
EntityState.Added : EntityState.Modified;
context.Entry(book.Author).State = book.Author.AuthorId == 0 ?
EntityState.Added : EntityState.Modified;
foreach (var cat in book.Categories)
{
context.Entry(cat).State = cat.CategoryId == 0 ?
EntityState.Added : EntityState.Modified;
}
context.SaveChanges();
}
}
public static void DisplayData(List<Author> list)
{
foreach(var author in list)
{
Console.WriteLine("Author Name: " + author.Name);
Console.WriteLine("\tBook List:");
foreach(var book in author.Books)
{
Console.WriteLine("\t\t" + book.Title);
}
}
}
public static void InsertData()
{
using (var context = new BookStore())
{
Category c1 = new Category() { CategoryName = "C#" };
Category c2 = new Category() { CategoryName = "Programming" };
Category c3 = new Category() { CategoryName = "Java" };
Category c4 = new Category() { CategoryName = "SQL" };
Category c5 = new Category() { CategoryName = "VB.NET" };
Author author1 = new Author()
{
Name = "Mark",
Books = new List<Book>
{
new Book() { Title = "Fundamentals of Computer Programming with C#", Categories = new List<Category>(){c1, c2}},
new Book() { Title = "Java: A Beginner's Guide", Categories = new List<Category>(){c3, c2}},
}
};
Author author2 = new Author()
{
Name = "Andy",
Books = new List<Book>
{
new Book() { Title = "SQL: The Ultimate Beginners Guide", Categories = new List<Category>(){c4}}
}
};
Author author3 = new Author()
{
Name = "Johny",
Books = new List<Book>
{
new Book() { Title = "Learn VB.NET", Categories = new List<Category>(){c5, c2}},
new Book() { Title = "C# Fundamentals for Absolute Beginners", Categories = new List<Category>(){c1, c2}},
}
};
context.Authors.Add(author1);
context.Authors.Add(author2);
context.Authors.Add(author3);
context.SaveChanges();
}
}
public class BookStore : DbContext
{
public BookStore() : base(FiddleHelper.GetConnectionStringSqlServer())
{
}
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<Category> Categories { get; set; }
}
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public int AuthorId { get; set; }
[ForeignKey("AuthorId")]
public Author Author { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Author
{
public int AuthorId { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
}