using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
using System.ComponentModel.DataAnnotations;
private static BooksContext _db;
private const string WARISDIRIE_AUTHORNAME = "Waris Dirie";
private const string CATHLENEMILLER_AUTHORNAME = "Cathlene Miller";
private const string DESERTFLOWER_BOOKTITLE = "KWIAT PUSTYNI";
public static void Main()
_db = new BooksContext();
int id = _db.Authors.Where(x => x.FullName == WARISDIRIE_AUTHORNAME).Select(x => x.Id).Single();
var authorDetails = GetAuthorById(id);
Console.WriteLine("Id of Author: {0}", id);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(authorDetails, new Newtonsoft.Json.JsonSerializerSettings
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(authorDetails.Authors_Books.First().Book, new Newtonsoft.Json.JsonSerializerSettings
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
public static Author GetAuthorById(int id)
var bookDetails = _db.Authors
.Include(ab => ab.Authors_Books)
.ThenInclude(b => b.Book)
.ThenInclude(book => book.Authors_Books)
.FirstOrDefault(n => n.Id == id);
private static void SeedDataContext()
var create = _db.Database.GenerateCreateScript();
_db.Database.EnsureCreated();
var warisDirieAuthor = _db.Authors.FirstOrDefault(x => x.FullName == WARISDIRIE_AUTHORNAME);
if (warisDirieAuthor == null)
warisDirieAuthor = new Author
FullName = WARISDIRIE_AUTHORNAME,
Bio = "Waris Dirie (Somali: Waris Diiriye) (born in 1965) is a Somali model, author, actress and human rights activist in the fight against Female Genital Mutilation (FGM). From 1997 to 2003, she was a UN special ambassador against female genital mutilation. In 2002 she founded her own organization in Vienna, the Desert Flower Foundation.",
ImageURL = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Waris_Dirie%2C_2018_during_her_visit_to_Sierra_Leone.jpg/220px-Waris_Dirie%2C_2018_during_her_visit_to_Sierra_Leone.jpg"
_db.Authors.Add(warisDirieAuthor);
var cathleneMillerAuthor = _db.Authors.FirstOrDefault(x => x.FullName == CATHLENEMILLER_AUTHORNAME);
if (cathleneMillerAuthor == null)
cathleneMillerAuthor = new Author
FullName = CATHLENEMILLER_AUTHORNAME ,
Bio = "Cathleen Miller is an internationally best-selling American nonfiction writer based in California. Her 2013 book, Champion of Choice, is the biography of United Nations leader Nafis Sadik." +
"she is also the author of Desert Flower (1998), co-written with Waris Dirie—a Somali nomad turned model turned activist—who shared her experience with female genital mutilation to bring about global awareness.[8][9] The book has been cited by the United Nations as having played a major role in the advocacy against female genital mutilation. Desert Flower was made into a feature film in 2009 and released in 34 countries. The print version sold over 11 million copies worldwide and has been translated into more than 55 languages.",
ImageURL = "https://www.svlaureates.org/wp-content/uploads/sites/www.svlaureates.org/images/2018/05/cathleen-miller-1024x1024-300x300.jpg"
_db.Authors.Add(cathleneMillerAuthor);
var dersertFlowerBook = _db.Books.FirstOrDefault(x => x.Title == DESERTFLOWER_BOOKTITLE);
if (dersertFlowerBook == null)
dersertFlowerBook = new Book { Title = DESERTFLOWER_BOOKTITLE };
dersertFlowerBook.Authors_Books.Add(new Author_Book { Author = warisDirieAuthor });
dersertFlowerBook.Authors_Books.Add(new Author_Book { Author = cathleneMillerAuthor });
_db.Books.Add(dersertFlowerBook);
public class BooksContext : DbContext
public DbSet<Book> Books { get; set; }
public DbSet<Author> Authors { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(FiddleHelper.GetConnectionStringSqlServer());
protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Book>().HasMany(b => b.Authors_Books).WithOne(ab => ab.Book).HasForeignKey(ab => ab.BookId);
modelBuilder.Entity<Author>().HasMany(a => a.Authors_Books).WithOne(ab => ab.Author).HasForeignKey(ab => ab.AuthorId);
public int Id { get; set; }
public string Title { get; set; }
public DateTime RelaseDate { get; set; }
public string Description { get; set; }
public string ImageURL { get; set; }
public bool IsBorrowed { get; set; }
public string ISBN { get; set; }
public virtual ICollection <Author_Book> Authors_Books { get; set; } = new HashSet<Author_Book>();
public int Id { get; set; }
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
public int BookId { get; set; }
public virtual Book Book { get; set; }
public int Id { get; set; }
[Display(Name = "Autor")]
[Required(ErrorMessage = "Author jest wymagany")]
public string FullName { get; set; }
[Display(Name = "Biografia")]
[Required(ErrorMessage = "Biografia jest wymagana")]
public string Bio { get; set; }
[Display(Name = "Zdjęcie")]
public string ImageURL { get; set; }
public virtual ICollection<Author_Book> Authors_Books { get; set; } = new HashSet<Author_Book>();