Skip to content

Commit f17a73f

Browse files
committed
Add solution code for Ch10 and 11
1 parent 8b0c2b1 commit f17a73f

39 files changed

+27943
-0
lines changed

code/Chapter10/Chapter10.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33829.357
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkingWithEFCore", "WorkingWithEFCore\WorkingWithEFCore.csproj", "{F370D9A0-3198-4D1B-AD9B-7F2C379EB169}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F370D9A0-3198-4D1B-AD9B-7F2C379EB169}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F370D9A0-3198-4D1B-AD9B-7F2C379EB169}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F370D9A0-3198-4D1B-AD9B-7F2C379EB169}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F370D9A0-3198-4D1B-AD9B-7F2C379EB169}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C8ECB07E-294D-4CB1-9FBA-8C884C06F650}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.ComponentModel.DataAnnotations.Schema;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace WorkingWithEFCore.AutoGen;
8+
9+
[Index("CategoryName", Name = "CategoryName")]
10+
public partial class Category
11+
{
12+
[Key]
13+
public int CategoryId { get; set; }
14+
15+
[Column(TypeName = "nvarchar (15)")]
16+
public string CategoryName { get; set; } = null!;
17+
18+
[Column(TypeName = "ntext")]
19+
public string? Description { get; set; }
20+
21+
[Column(TypeName = "image")]
22+
public byte[]? Picture { get; set; }
23+
24+
[InverseProperty("Category")]
25+
public virtual ICollection<Product> Products { get; set; } = new List<Product>();
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace WorkingWithEFCore.AutoGen;
6+
7+
public partial class NorthwindDb : DbContext
8+
{
9+
public NorthwindDb()
10+
{
11+
}
12+
13+
public NorthwindDb(DbContextOptions<NorthwindDb> options)
14+
: base(options)
15+
{
16+
}
17+
18+
public virtual DbSet<Category> Categories { get; set; }
19+
20+
public virtual DbSet<Product> Products { get; set; }
21+
22+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
23+
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
24+
=> optionsBuilder.UseSqlite("Data Source=Northwind.db");
25+
26+
protected override void OnModelCreating(ModelBuilder modelBuilder)
27+
{
28+
modelBuilder.Entity<Category>(entity =>
29+
{
30+
entity.Property(e => e.CategoryId).ValueGeneratedNever();
31+
});
32+
33+
modelBuilder.Entity<Product>(entity =>
34+
{
35+
entity.Property(e => e.ProductId).ValueGeneratedNever();
36+
entity.Property(e => e.Discontinued).HasDefaultValueSql("0");
37+
entity.Property(e => e.ReorderLevel).HasDefaultValueSql("0");
38+
entity.Property(e => e.UnitPrice).HasDefaultValueSql("0");
39+
entity.Property(e => e.UnitsInStock).HasDefaultValueSql("0");
40+
entity.Property(e => e.UnitsOnOrder).HasDefaultValueSql("0");
41+
});
42+
43+
OnModelCreatingPartial(modelBuilder);
44+
}
45+
46+
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.ComponentModel.DataAnnotations.Schema;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace WorkingWithEFCore.AutoGen;
8+
9+
[Index("CategoryId", Name = "CategoriesProducts")]
10+
[Index("CategoryId", Name = "CategoryId")]
11+
[Index("ProductName", Name = "ProductName")]
12+
[Index("SupplierId", Name = "SupplierId")]
13+
[Index("SupplierId", Name = "SuppliersProducts")]
14+
public partial class Product
15+
{
16+
[Key]
17+
public int ProductId { get; set; }
18+
19+
[Column(TypeName = "nvarchar (40)")]
20+
public string ProductName { get; set; } = null!;
21+
22+
[Column(TypeName = "INT")]
23+
public int? SupplierId { get; set; }
24+
25+
[Column(TypeName = "INT")]
26+
public int? CategoryId { get; set; }
27+
28+
[Column(TypeName = "nvarchar (20)")]
29+
public string? QuantityPerUnit { get; set; }
30+
31+
[Column(TypeName = "money")]
32+
public double? UnitPrice { get; set; }
33+
34+
[Column(TypeName = "smallint")]
35+
public short? UnitsInStock { get; set; }
36+
37+
[Column(TypeName = "smallint")]
38+
public short? UnitsOnOrder { get; set; }
39+
40+
[Column(TypeName = "smallint")]
41+
public short? ReorderLevel { get; set; }
42+
43+
[Required]
44+
[Column(TypeName = "bit")]
45+
public bool? Discontinued { get; set; }
46+
47+
[ForeignKey("CategoryId")]
48+
[InverseProperty("Products")]
49+
public virtual Category? Category { get; set; }
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.ComponentModel.DataAnnotations.Schema; // To use [Column].
2+
3+
namespace Northwind.EntityModels;
4+
5+
public class Category
6+
{
7+
// These properties map to columns in the database.
8+
public int CategoryId { get; set; } // The primary key.
9+
10+
public string CategoryName { get; set; } = null!;
11+
12+
[Column(TypeName = "ntext")]
13+
public string? Description { get; set; }
14+
15+
// Defines a navigation property for related rows.
16+
public virtual ICollection<Product> Products { get; set; }
17+
// To enable developers to add products to a Category, we must
18+
// initialize the navigation property to an empty collection.
19+
= new HashSet<Product>();
20+
}
552 KB
Binary file not shown.

0 commit comments

Comments
 (0)