Skip to content

Commit f981339

Browse files
ef core, menu to menuitem
1 parent 2a9a4be commit f981339

21 files changed

+122
-116
lines changed

2_Libs/EFCore/Cosmos/Menu.cs

-11
This file was deleted.

2_Libs/EFCore/Cosmos/MenuCard.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public MenuCard(string title, string restaurantId, Guid menuCardId = default)
88

99
public Guid MenuCardId { get; set; }
1010
public string Title { get; set; }
11-
public ICollection<Menu> Menus { get; internal set; } = new HashSet<Menu>();
11+
public ICollection<MenuItem> MenuItems { get; internal set; } = new HashSet<MenuItem>();
1212
public string RestaurantId { get; set; }
1313
public bool IsActive { get; set; } = true;
1414
public override string ToString() => Title;

2_Libs/EFCore/Cosmos/MenuItem.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
public class MenuItem
4+
{
5+
public MenuItem(string text, Guid menuItemId = default) => (Text, MenuItemId) = (text, menuItemId);
6+
7+
public Guid MenuItemId { get; set; }
8+
public string Text { get; set; }
9+
public decimal? Price { get; set; }
10+
11+
public override string ToString() => Text;
12+
}

2_Libs/EFCore/Cosmos/MenusContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
1717
{
1818
modelBuilder.HasDefaultContainer("menucards");
1919

20-
modelBuilder.Entity<MenuCard>().OwnsMany(c => c.Menus);
20+
modelBuilder.Entity<MenuCard>().OwnsMany(c => c.MenuItems);
2121
modelBuilder.Entity<MenuCard>().HasKey(c => c.MenuCardId);
2222

2323
modelBuilder.Entity<MenuCard>().HasPartitionKey(c => c.RestaurantId);

2_Libs/EFCore/Cosmos/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
var runner = scope.ServiceProvider.GetRequiredService<Runner>();
2323
await runner.CreateDatabaseAsync();
2424

25-
// await runner.AddMenuCardAsync();
25+
await runner.AddMenuCardAsync();
2626
await runner.AddAddtionalCardsAsync();
2727
await runner.ShowCardsAsync();
2828
await runner.DeleteDatabaseAsync();

2_Libs/EFCore/Cosmos/Runner.cs

+9-12
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ public async Task AddMenuCardAsync()
3131
Console.WriteLine(nameof(AddMenuCardAsync));
3232
MenuCard soupCard = new("Soups", _restaurantId);
3333

34-
Menu[] soups = new[]
34+
MenuItem[] soups = new MenuItem[]
3535
{
36-
new Menu("Consommé Célestine (with shredded pancake)")
36+
new("Consommé Célestine (with shredded pancake)")
3737
{
3838
Price = 4.8m
3939
},
40-
new Menu("Baked Potato Soup")
40+
new("Baked Potato Soup")
4141
{
4242
Price = 4.8m
4343
},
44-
new Menu("Cheddar Broccoli Soup")
44+
new("Cheddar Broccoli Soup")
4545
{
4646
Price = 4.8m
4747
}
4848
};
4949

5050
foreach (var soup in soups)
5151
{
52-
soupCard.Menus.Add(soup);
52+
soupCard.MenuItems.Add(soup);
5353
}
5454

5555
_menusContext.MenuCards.Add(soupCard);
@@ -62,8 +62,8 @@ public async Task AddMenuCardAsync()
6262
public async Task AddAddtionalCardsAsync()
6363
{
6464
Random random = new();
65-
var menus = Enumerable.Range(1, 10).Select(i => new Menu($"menu {i}") { Price = random.Next(8) }).ToList();
66-
var cards = Enumerable.Range(1, 5).Select(i => new MenuCard($"card {i}", _restaurantId) { Menus = menus });
65+
var menus = Enumerable.Range(1, 10).Select(i => new MenuItem($"menu {i}") { Price = random.Next(8) }).ToList();
66+
var cards = Enumerable.Range(1, 5).Select(i => new MenuCard($"card {i}", _restaurantId) { MenuItems = menus });
6767

6868
await _menusContext.MenuCards.AddRangeAsync(cards);
6969
await _menusContext.SaveChangesAsync();
@@ -79,9 +79,9 @@ public async Task ShowCardsAsync()
7979
foreach (var card in cards)
8080
{
8181
Console.WriteLine(card.Title);
82-
foreach (var menu in card.Menus)
82+
foreach (var menuItem in card.MenuItems)
8383
{
84-
Console.WriteLine(menu.Text);
84+
Console.WriteLine(menuItem.Text);
8585
}
8686
}
8787
}
@@ -97,7 +97,4 @@ public async Task DeleteDatabaseAsync()
9797
Console.WriteLine($"database {deletionInfo}");
9898
}
9999
}
100-
101-
102100
}
103-

2_Libs/EFCore/Queries/CompiledQueryExtensions.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@
55

66
static class CompiledQueryExtensions
77
{
8-
private static Func<MenusContext, string, IEnumerable<Menu>>? s_menusByText;
8+
private static Func<MenusContext, string, IEnumerable<MenuItem>>? s_menuItemsByText;
99

10-
private static Func<MenusContext, string, IEnumerable<Menu>> CompileMenusByTextQuery()
10+
private static Func<MenusContext, string, IEnumerable<MenuItem>> CompileMenusByTextQuery()
1111
=> EF.CompileQuery((MenusContext context, string text)
12-
=> context.Menus.Where(m => m.Text == text));
12+
=> context.MenuItems.Where(m => m.Text == text));
1313

14-
public static IEnumerable<Menu> MenusByText(this MenusContext menusContext, string text)
14+
public static IEnumerable<MenuItem> MenuItemsByText(this MenusContext menusContext, string text)
1515
{
16-
if (s_menusByText is null)
16+
if (s_menuItemsByText is null)
1717
{
18-
s_menusByText = CompileMenusByTextQuery();
18+
s_menuItemsByText = CompileMenusByTextQuery();
1919
}
20-
return s_menusByText(menusContext, text);
20+
return s_menuItemsByText(menusContext, text);
2121
}
2222

23-
private static Func<MenusContext, string, IAsyncEnumerable<Menu>>? s_menusByTextAsync;
24-
private static Func<MenusContext, string, IAsyncEnumerable<Menu>> CompileMenusByTextAsyncQuery()
23+
private static Func<MenusContext, string, IAsyncEnumerable<MenuItem>>? s_menuItemsByTextAsync;
24+
private static Func<MenusContext, string, IAsyncEnumerable<MenuItem>> CompileMenuItemsByTextAsyncQuery()
2525
=> EF.CompileAsyncQuery((MenusContext context, string text)
26-
=> context.Menus.Where(m => m.Text == text));
26+
=> context.MenuItems.Where(m => m.Text == text));
2727

28-
public static IAsyncEnumerable<Menu> MenusByTextAsync(this MenusContext menusContext, string text)
28+
public static IAsyncEnumerable<MenuItem> MenuItemsByTextAsync(this MenusContext menusContext, string text)
2929
{
30-
if (s_menusByTextAsync is null)
30+
if (s_menuItemsByTextAsync is null)
3131
{
32-
s_menusByTextAsync = CompileMenusByTextAsyncQuery();
32+
s_menuItemsByTextAsync = CompileMenuItemsByTextAsyncQuery();
3333
}
34-
return s_menusByTextAsync(menusContext, text);
34+
return s_menuItemsByTextAsync(menusContext, text);
3535
}
3636
}

2_Libs/EFCore/Queries/MenuCard.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public MenuCard(string title, int menuCardId = default)
77

88
public int MenuCardId { get; set; }
99
public string Title { get; set; }
10-
public ICollection<Menu> Menus { get; } = new List<Menu>();
10+
public ICollection<MenuItem> Menus { get; } = new List<MenuItem>();
1111
public override string ToString() => Title;
1212
}

2_Libs/EFCore/Queries/MenuConfiguration.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System;
44
using static ColumnNames;
55

6-
internal class MenuConfiguration : IEntityTypeConfiguration<Menu>
6+
internal class MenuConfiguration : IEntityTypeConfiguration<MenuItem>
77
{
8-
public void Configure(EntityTypeBuilder<Menu> builder)
8+
public void Configure(EntityTypeBuilder<MenuItem> builder)
99
{
1010
builder.ToTable("Menus")
1111
.HasKey(m => m.MenuId);

2_Libs/EFCore/Queries/Menu.cs 2_Libs/EFCore/Queries/MenuItem.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22

3-
public class Menu
3+
public class MenuItem
44
{
5-
public Menu(string text, int menuId = default) => (Text, MenuId) = (text, menuId);
5+
public MenuItem(string text, int menuId = default) => (Text, MenuId) = (text, menuId);
66

77
public int MenuId { get; set; }
88
public string Text { get; set; }

2_Libs/EFCore/Queries/MenusContext.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public MenusContext(DbContextOptions<MenusContext> options)
1212
: base(options) {}
1313

1414
public DbSet<MenuCard> MenuCards => Set<MenuCard>();
15-
public DbSet<Menu> Menus => Set<Menu>();
15+
public DbSet<MenuItem> MenuItems => Set<MenuItem>();
1616
public DbSet<Restaurant> Restaurants => Set<Restaurant>();
1717

1818
protected override void OnModelCreating(ModelBuilder modelBuilder)
@@ -44,9 +44,9 @@ private void SeedData(ModelBuilder modelBuilder)
4444
var menus1 = GetInitialMenus(card1, restaurantId, 1, 20);
4545
var menus2 = GetInitialMenus(card2, restaurantId, 21, 20);
4646
modelBuilder.Entity<MenuCard>().HasData(card1);
47-
modelBuilder.Entity<Menu>().HasData(menus1);
47+
modelBuilder.Entity<MenuItem>().HasData(menus1);
4848
modelBuilder.Entity<MenuCard>().HasData(card2);
49-
modelBuilder.Entity<Menu>().HasData(menus2);
49+
modelBuilder.Entity<MenuItem>().HasData(menus2);
5050
}
5151

5252
private IEnumerable<dynamic> GetInitialMenus(dynamic card, Guid restaurantId, int start, int count)
@@ -68,7 +68,7 @@ public override Task<int> SaveChangesAsync(CancellationToken cancellationToken =
6868
{
6969
ChangeTracker.DetectChanges();
7070

71-
foreach (var item in ChangeTracker.Entries<Menu>()
71+
foreach (var item in ChangeTracker.Entries<MenuItem>()
7272
.Where(e => e.State == EntityState.Added
7373
|| e.State == EntityState.Modified
7474
|| e.State == EntityState.Deleted))

2_Libs/EFCore/Queries/Runner.cs

+21-21
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,59 @@ public Task CreateDatabaseAsync()
2020
public async Task FindByKeyAsync(int id)
2121
{
2222
Console.WriteLine(nameof(FindByKeyAsync));
23-
Menu? menu = await _menusContext.Menus.FindAsync(id);
24-
Console.WriteLine(menu);
23+
MenuItem? menuItem = await _menusContext.MenuItems.FindAsync(id);
24+
Console.WriteLine(menuItem);
2525
Console.WriteLine();
2626
}
2727

2828
public async Task SingleOrDefaultAsync(string text)
2929
{
3030
Console.WriteLine(nameof(SingleOrDefaultAsync));
31-
Menu? menu = await _menusContext.Menus.TagWith("SingleOrDefault").SingleOrDefaultAsync(m => m.Text == text);
31+
MenuItem? menu = await _menusContext.MenuItems.TagWith("SingleOrDefault").SingleOrDefaultAsync(m => m.Text == text);
3232
Console.WriteLine(menu);
3333
Console.WriteLine();
3434
}
3535

3636
public async Task FirstOrDefaultAsync(string title)
3737
{
3838
Console.WriteLine(nameof(SingleOrDefaultAsync));
39-
Menu? menu = await _menusContext.Menus.TagWith("FirstOrDefault").FirstOrDefaultAsync(m => m.Text == title);
39+
MenuItem? menu = await _menusContext.MenuItems.TagWith("FirstOrDefault").FirstOrDefaultAsync(m => m.Text == title);
4040
Console.WriteLine(menu);
4141
Console.WriteLine();
4242
}
4343

4444
public async Task WhereAsync()
4545
{
4646
Console.WriteLine(nameof(WhereAsync));
47-
var menus = await _menusContext.Menus.Where(m => m.Text.Contains("menu")).TagWith("Where").ToListAsync();
48-
foreach (var menu in menus)
47+
var menuItems = await _menusContext.MenuItems.Where(m => m.Text.Contains("menu")).TagWith("Where").ToListAsync();
48+
foreach (var menuItem in menuItems)
4949
{
50-
Console.WriteLine(menu);
50+
Console.WriteLine(menuItem);
5151
}
5252
Console.WriteLine();
5353
}
5454

5555
public async Task PagingAsync(int skip, int take)
5656
{
5757
Console.WriteLine(nameof(PagingAsync));
58-
var menus = await _menusContext.Menus
58+
var menuItems = await _menusContext.MenuItems
5959
.OrderBy(m => m.MenuId)
6060
.Skip(skip)
6161
.Take(take)
6262
.TagWith("SkipAndTake")
6363
.ToListAsync();
64-
foreach (var menu in menus)
64+
foreach (var menuItem in menuItems)
6565
{
66-
Console.WriteLine(menu);
66+
Console.WriteLine(menuItem);
6767
}
6868

6969
Console.WriteLine();
7070
}
7171

7272
public async Task GetAllMenusUsingAsyncStream()
7373
{
74-
IAsyncEnumerable<Menu> menus = _menusContext.Menus.AsAsyncEnumerable();
75-
await foreach (var menu in menus)
74+
IAsyncEnumerable<MenuItem> menuItems = _menusContext.MenuItems.AsAsyncEnumerable();
75+
await foreach (var menu in menuItems)
7676
{
7777
Console.WriteLine(menu);
7878
}
@@ -81,8 +81,8 @@ public async Task GetAllMenusUsingAsyncStream()
8181
public async Task RawSqlAsync(string term)
8282
{
8383
Console.WriteLine(nameof(RawSqlAsync));
84-
var menus = await _menusContext.Menus.FromSqlInterpolated($"SELECT * FROM [mc].[Menus] WHERE TEXT = '{term}'").TagWith("RawSQL").ToListAsync();
85-
foreach (var menu in menus)
84+
var menuItems = await _menusContext.MenuItems.FromSqlInterpolated($"SELECT * FROM [mc].[Menus] WHERE TEXT = '{term}'").TagWith("RawSQL").ToListAsync();
85+
foreach (var menu in menuItems)
8686
{
8787
Console.WriteLine(menu);
8888
}
@@ -93,8 +93,8 @@ public async Task RawSqlAsync(string term)
9393
public void UseCompiledQuery()
9494
{
9595
Console.WriteLine(nameof(UseCompiledQuery));
96-
var menus = _menusContext.MenusByText("menu 26");
97-
foreach (var menu in menus)
96+
var menuItems = _menusContext.MenuItemsByText("menu 26");
97+
foreach (var menu in menuItems)
9898
{
9999
Console.WriteLine(menu);
100100
}
@@ -105,9 +105,9 @@ public async Task UseCompiledQueryAsync()
105105
{
106106
Console.WriteLine(nameof(UseCompiledQueryAsync));
107107

108-
await foreach (var menu in _menusContext.MenusByTextAsync("menu 26"))
108+
await foreach (var menuItem in _menusContext.MenuItemsByTextAsync("menu 26"))
109109
{
110-
Console.WriteLine(menu);
110+
Console.WriteLine(menuItem);
111111
}
112112
Console.WriteLine();
113113
}
@@ -116,10 +116,10 @@ public async Task UseEFCunctions(string textSegment)
116116
Console.WriteLine(nameof(UseEFCunctions));
117117
string likeExpression = $"%{textSegment}%";
118118

119-
var menus = await _menusContext.Menus.Where(m => EF.Functions.Like(m.Text, likeExpression)).ToListAsync();
120-
foreach (var menu in menus)
119+
var menuItems = await _menusContext.MenuItems.Where(m => EF.Functions.Like(m.Text, likeExpression)).ToListAsync();
120+
foreach (var menuItem in menuItems)
121121
{
122-
Console.WriteLine(menu);
122+
Console.WriteLine(menuItem);
123123
}
124124
Console.WriteLine();
125125
}

2_Libs/EFCore/README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Readme - Code Samples for Chapter 26, Entity Framework Core
1+
# Readme - Code Samples for Chapter 21, Entity Framework Core
22

33
The sample code for this chapter contains this solution:
44

@@ -7,7 +7,6 @@ The sample code for this chapter contains this solution:
77
consisting of these sample projects
88

99
* Intro (introduction to EF Core with models, contexts, creating the database, read, write, update, delete, logging)
10-
* UsingDependencyInjection (using Microsoft.Extensions.DependencyInjection to create the context)
1110
* MenusSample (model with a relation, self contained type configuration, tracking, batch)
1211
* MenusWithDataAnnotations (using data annotation to create a model)
1312
* ScaffoldSmaple (using `dotnet ef` for scaffolding)
@@ -24,16 +23,11 @@ consisting of these sample projects
2423
* TransactionsSample (explicit transactions)
2524
* MigrationsLib/ConsoleApp/WebApp (migrations with dependency injection and a library)
2625

27-
To build and run the .NET Core samples, please install
28-
29-
* Visual Studio 2019 Update 8 with the .NET Core workload
3026

3127
To access the database, you can use the **Azure Data Studio** on Winodws, MacOS, and Linux:
3228
[Download and install Azure Data Studio](https://docs.microsoft.com/en-us/sql/azure-data-studio/download-azure-data-studio)
33-
34-
Please download and install the tools from [.NET Core downloads](https://www.microsoft.com/net/core).
3529

36-
For code comments and issues please check [Professional C#'s GitHub Repository](https://github.com/ProfessionalCSharp/ProfessionalCSharp7)
30+
For code comments and issues please check [Professional C#'s GitHub Repository](https://github.com/ProfessionalCSharp/ProfessionalCSharp2021)
3731

3832
Please check my blog [csharp.christiannagel.com](https://csharp.christiannagel.com "csharp.christiannagel.com") for additional information for topics covered in the book.
3933

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
public class Menu
1+
public class MenuItem
22
{
3-
public Menu(string title, int menuId = 0)
3+
public MenuItem(string title, int menuItemId = 0)
44
{
55
Title = title;
6-
MenuId = menuId;
6+
MenuItemId = menuItemId;
77
}
8-
public int MenuId { get; set; }
8+
public int MenuItemId { get; set; }
99
public string Title { get; set; }
1010
public string? Subtitle { get; set; }
1111
public decimal Price { get; set; }
@@ -17,5 +17,5 @@ public class MenuDetails
1717
public int MenuDetailsId { get; set; }
1818
public string? KitchenInfo { get; set; }
1919
public int MenusSold { get; set; }
20-
public Menu? Menu { get; set; }
20+
public MenuItem? Menu { get; set; }
2121
}

0 commit comments

Comments
 (0)