1- using EntityFrameworkCore . Projectables ;
2- using EntityFrameworkCore . Projectables . Extensions ;
3- using Microsoft . Data . Sqlite ;
4- using Microsoft . EntityFrameworkCore ;
5- using Microsoft . Extensions . Caching . Memory ;
6- using Microsoft . Extensions . DependencyInjection ;
7- using Microsoft . Extensions . Logging ;
8- using System ;
9- using System . Collections ;
1+ using System ;
102using System . Collections . Generic ;
113using System . ComponentModel . DataAnnotations . Schema ;
12- using System . Diagnostics ;
134using System . Linq ;
5+ using EntityFrameworkCore . Projectables ;
6+ using Microsoft . Data . Sqlite ;
7+ using Microsoft . EntityFrameworkCore ;
8+ using Microsoft . Extensions . DependencyInjection ;
149
1510namespace BasicSample
1611{
@@ -22,12 +17,14 @@ public class User
2217
2318 public ICollection < Order > Orders { get ; set ; }
2419
25- [ Projectable ]
26- public string FullName
27- => FirstName + " " + LastName ;
20+ [ Projectable ( UseMemberBody = nameof ( _FullName ) ) ]
21+ public string FullName { get ; set ; }
22+ private string _FullName => FirstName + " " + LastName ;
2823
29- [ Projectable ]
30- public double TotalSpent => Orders . Sum ( x => x . PriceSum ) ;
24+ [ Projectable ( UseMemberBody = nameof ( _TotalSpent ) ) ]
25+ [ NotMapped ]
26+ public double TotalSpent { get ; set ; }
27+ private double _TotalSpent => Orders . Sum ( x => x . PriceSum ) ;
3128
3229 [ Projectable ]
3330 public Order MostValuableOrder
@@ -86,7 +83,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
8683
8784 class Program
8885 {
89- static void Main ( string [ ] args )
86+ public static void Main ( string [ ] args )
9087 {
9188 using var dbConnection = new SqliteConnection ( "Filename=:memory:" ) ;
9289 dbConnection . Open ( ) ;
@@ -95,6 +92,8 @@ static void Main(string[] args)
9592 . AddDbContext < ApplicationDbContext > ( ( provider , options ) => {
9693 options
9794 . UseSqlite ( dbConnection )
95+ . LogTo ( Console . WriteLine )
96+ . EnableSensitiveDataLogging ( )
9897 . UseProjectables ( ) ;
9998 } )
10099 . BuildServiceProvider ( ) ;
@@ -105,9 +104,9 @@ static void Main(string[] args)
105104 var product1 = new Product { Name = "Red pen" , Price = 1.5 } ;
106105 var product2 = new Product { Name = "Blue pen" , Price = 2.1 } ;
107106
108- var user = new User {
109- FirstName = "Jon" ,
110- LastName = "Doe" ,
107+ var user = new User {
108+ FirstName = "Jon" ,
109+ LastName = "Doe" ,
111110 Orders = new List < Order > {
112111 new Order {
113112 Items = new List < OrderItem > {
@@ -130,6 +129,19 @@ static void Main(string[] args)
130129 dbContext . SaveChanges ( ) ;
131130
132131 // What did our user spent in total
132+
133+ {
134+ foreach ( var u in dbContext . Users )
135+ {
136+ Console . WriteLine ( $ "User name: { u . FullName } ") ;
137+ }
138+ }
139+
140+ {
141+ var result = dbContext . Users . FirstOrDefault ( ) ;
142+ Console . WriteLine ( $ "Our first user { result . FullName } has spent { result . TotalSpent } ") ;
143+ }
144+
133145 {
134146 var query = dbContext . Users
135147 . Select ( x => new {
0 commit comments