-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d308e88
commit d916a76
Showing
23 changed files
with
1,029 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+791 KB
07.Advanced Querying/07. DB-Advanced-EF-Core-Advanced-Querying-Exercises.docx
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;"; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
07.Advanced Querying/BookShop.Data/EntityConfiguration/AuthorConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
07.Advanced Querying/BookShop.Data/EntityConfiguration/BookCategoryConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
07.Advanced Querying/BookShop.Data/EntityConfiguration/BookConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
07.Advanced Querying/BookShop.Data/EntityConfiguration/CategoryConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
07.Advanced Querying/BookShop.Initializer/BookShop.Initializer.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
07.Advanced Querying/BookShop.Initializer/DbInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
07.Advanced Querying/BookShop.Initializer/Generators/AuthorGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.