Skip to content

Commit d916a76

Browse files
committed
ADVANCED QUERYING
1 parent d308e88 commit d916a76

23 files changed

+1029
-0
lines changed
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="2.2.0" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\BookShop.Models\BookShop.Models.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace BookShop.Data
2+
{
3+
using Microsoft.EntityFrameworkCore;
4+
5+
using Models;
6+
using EntityConfiguration;
7+
8+
public class BookShopContext : DbContext
9+
{
10+
public BookShopContext() { }
11+
12+
public BookShopContext(DbContextOptions options)
13+
:base(options) { }
14+
15+
public DbSet<Book> Books { get; set; }
16+
public DbSet<Category> Categories { get; set; }
17+
public DbSet<Author> Authors { get; set; }
18+
public DbSet<BookCategory> BooksCategories { get; set; }
19+
20+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
21+
{
22+
if (!optionsBuilder.IsConfigured)
23+
{
24+
optionsBuilder.UseSqlServer(Configuration.ConnectionString);
25+
}
26+
}
27+
28+
protected override void OnModelCreating(ModelBuilder modelBuilder)
29+
{
30+
modelBuilder.ApplyConfiguration(new AuthorConfiguration());
31+
modelBuilder.ApplyConfiguration(new BookCategoryConfiguration());
32+
modelBuilder.ApplyConfiguration(new BookConfiguration());
33+
modelBuilder.ApplyConfiguration(new CategoryConfiguration());
34+
}
35+
}
36+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BookShop.Data
2+
{
3+
internal class Configuration
4+
{
5+
internal static string ConnectionString => "Server=.;Database=BookShop;Integrated Security=True;";
6+
}
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace BookShop.Data.EntityConfiguration
2+
{
3+
using Models;
4+
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
7+
8+
internal class AuthorConfiguration : IEntityTypeConfiguration<Author>
9+
{
10+
public void Configure(EntityTypeBuilder<Author> builder)
11+
{
12+
builder.HasKey(e => e.AuthorId);
13+
14+
builder.Property(e => e.FirstName)
15+
.IsRequired(false)
16+
.HasMaxLength(50);
17+
18+
builder.Property(e => e.LastName)
19+
.IsRequired()
20+
.HasMaxLength(50);
21+
}
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace BookShop.Data.EntityConfiguration
2+
{
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
5+
6+
using Models;
7+
8+
internal class BookCategoryConfiguration : IEntityTypeConfiguration<BookCategory>
9+
{
10+
public void Configure(EntityTypeBuilder<BookCategory> builder)
11+
{
12+
builder.HasKey(e => new { e.BookId, e.CategoryId });
13+
14+
builder.HasOne(e => e.Category)
15+
.WithMany(c => c.CategoryBooks)
16+
.HasForeignKey(e => e.CategoryId);
17+
18+
builder.HasOne(e => e.Book)
19+
.WithMany(c => c.BookCategories)
20+
.HasForeignKey(e => e.BookId);
21+
}
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace BookShop.Data.EntityConfiguration
2+
{
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
5+
6+
using Models;
7+
8+
internal class BookConfiguration : IEntityTypeConfiguration<Book>
9+
{
10+
public void Configure(EntityTypeBuilder<Book> builder)
11+
{
12+
builder.HasKey(e => e.BookId);
13+
14+
builder.Property(e => e.Title)
15+
.IsRequired()
16+
.HasMaxLength(50);
17+
18+
builder.Property(e => e.Description)
19+
.IsRequired()
20+
.HasMaxLength(1000);
21+
22+
builder.Property(e => e.ReleaseDate)
23+
.IsRequired(false);
24+
25+
builder.HasOne(e => e.Author)
26+
.WithMany(a => a.Books)
27+
.HasForeignKey(e => e.AuthorId);
28+
}
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace BookShop.Data.EntityConfiguration
2+
{
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
5+
6+
using Models;
7+
8+
internal class CategoryConfiguration : IEntityTypeConfiguration<Category>
9+
{
10+
public void Configure(EntityTypeBuilder<Category> builder)
11+
{
12+
builder.HasKey(e => e.CategoryId);
13+
14+
builder.Property(e => e.Name)
15+
.IsRequired()
16+
.HasMaxLength(50);
17+
}
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\BookShop.Data\BookShop.Data.csproj" />
9+
<ProjectReference Include="..\BookShop.Models\BookShop.Models.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace BookShop.Initializer
2+
{
3+
using System;
4+
5+
using Data;
6+
using Models;
7+
using Generators;
8+
9+
public class DbInitializer
10+
{
11+
public static void ResetDatabase(BookShopContext context)
12+
{
13+
context.Database.EnsureDeleted();
14+
context.Database.EnsureCreated();
15+
16+
Console.WriteLine("BookShop database created successfully.");
17+
18+
Seed(context);
19+
}
20+
21+
public static void Seed(BookShopContext context)
22+
{
23+
Book[] books = BookGenerator.CreateBooks();
24+
25+
context.Books.AddRange(books);
26+
27+
context.SaveChanges();
28+
29+
Console.WriteLine("Sample data inserted successfully.");
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)