Skip to content

Commit

Permalink
ADVANCED QUERYING
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicTheCat committed Mar 15, 2019
1 parent d308e88 commit d916a76
Show file tree
Hide file tree
Showing 23 changed files with 1,029 additions and 0 deletions.
Binary file not shown.
16 changes: 16 additions & 0 deletions 07.Advanced Querying/BookShop.Data/BookShop.Data.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BookShop.Models\BookShop.Models.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions 07.Advanced Querying/BookShop.Data/BookShopContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace BookShop.Data
{
using Microsoft.EntityFrameworkCore;

using Models;
using EntityConfiguration;

public class BookShopContext : DbContext
{
public BookShopContext() { }

public BookShopContext(DbContextOptions options)
:base(options) { }

public DbSet<Book> Books { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<BookCategory> BooksCategories { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(Configuration.ConnectionString);
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new AuthorConfiguration());
modelBuilder.ApplyConfiguration(new BookCategoryConfiguration());
modelBuilder.ApplyConfiguration(new BookConfiguration());
modelBuilder.ApplyConfiguration(new CategoryConfiguration());
}
}
}
7 changes: 7 additions & 0 deletions 07.Advanced Querying/BookShop.Data/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BookShop.Data
{
internal class Configuration
{
internal static string ConnectionString => "Server=.;Database=BookShop;Integrated Security=True;";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace BookShop.Data.EntityConfiguration
{
using Models;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

internal class AuthorConfiguration : IEntityTypeConfiguration<Author>
{
public void Configure(EntityTypeBuilder<Author> builder)
{
builder.HasKey(e => e.AuthorId);

builder.Property(e => e.FirstName)
.IsRequired(false)
.HasMaxLength(50);

builder.Property(e => e.LastName)
.IsRequired()
.HasMaxLength(50);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace BookShop.Data.EntityConfiguration
{
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

using Models;

internal class BookCategoryConfiguration : IEntityTypeConfiguration<BookCategory>
{
public void Configure(EntityTypeBuilder<BookCategory> builder)
{
builder.HasKey(e => new { e.BookId, e.CategoryId });

builder.HasOne(e => e.Category)
.WithMany(c => c.CategoryBooks)
.HasForeignKey(e => e.CategoryId);

builder.HasOne(e => e.Book)
.WithMany(c => c.BookCategories)
.HasForeignKey(e => e.BookId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace BookShop.Data.EntityConfiguration
{
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

using Models;

internal class BookConfiguration : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
builder.HasKey(e => e.BookId);

builder.Property(e => e.Title)
.IsRequired()
.HasMaxLength(50);

builder.Property(e => e.Description)
.IsRequired()
.HasMaxLength(1000);

builder.Property(e => e.ReleaseDate)
.IsRequired(false);

builder.HasOne(e => e.Author)
.WithMany(a => a.Books)
.HasForeignKey(e => e.AuthorId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BookShop.Data.EntityConfiguration
{
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

using Models;

internal class CategoryConfiguration : IEntityTypeConfiguration<Category>
{
public void Configure(EntityTypeBuilder<Category> builder)
{
builder.HasKey(e => e.CategoryId);

builder.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BookShop.Data\BookShop.Data.csproj" />
<ProjectReference Include="..\BookShop.Models\BookShop.Models.csproj" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions 07.Advanced Querying/BookShop.Initializer/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace BookShop.Initializer
{
using System;

using Data;
using Models;
using Generators;

public class DbInitializer
{
public static void ResetDatabase(BookShopContext context)
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();

Console.WriteLine("BookShop database created successfully.");

Seed(context);
}

public static void Seed(BookShopContext context)
{
Book[] books = BookGenerator.CreateBooks();

context.Books.AddRange(books);

context.SaveChanges();

Console.WriteLine("Sample data inserted successfully.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace BookShop.Initializer.Generators
{
using BookShop.Models;

internal class AuthorGenerator
{
internal static Author[] CreateAuthors()
{
string[] authorNames = new string[]
{
"Nayden Vitanov",
"Deyan Tanev",
"Desislav Petkov",
"Dyakon Hristov",
"Milen Todorov",
"Aleksander Kishishev",
"Ilian Stoev",
"Milen Balkanski",
"Kostadin Nakov",
"Petar Strashilov",
"Bozhidara Valova",
"Lyubina Kostova",
"Radka Antonova",
"Vladimira Blagoeva",
"Bozhidara Rysinova",
"Borislava Dimitrova",
"Anelia Velichkova",
"Violeta Kochanova",
"Lyubov Ivanova",
"Blagorodna Dineva",
"Desislav Bachev",
"Mihael Borisov",
"Ventsislav Petrova",
"Hristo Kirilov",
"Penko Dachev",
"Nikolai Zhelyaskov",
"Petar Tsvetanov",
"Spas Dimitrov",
"Stanko Popov",
"Miro Kochanov",
"Pesho Stamatov",
"Roger Porter",
"Jeffrey Snyder",
"Louis Coleman",
"George Powell",
"Jane Ortiz",
"Randy Morales",
"Lisa Davis",

};

int authorCount = authorNames.Length;

Author[] authors = new Author[authorCount];

for (int i = 0; i < authorCount; i++)
{
string[] authorNameTokens = authorNames[i].Split();

Author author = new Author()
{
FirstName = authorNameTokens[0],
LastName = authorNameTokens[1],
};

authors[i] = author;
}

return authors;
}
}
}
Loading

0 comments on commit d916a76

Please sign in to comment.