Skip to content

Commit 19829cc

Browse files
committed
Add some failing tests
1 parent 669b02a commit 19829cc

2 files changed

Lines changed: 32 additions & 20 deletions

File tree

samples/BasicSample/BasicSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net7.0</TargetFramework>
66
<Nullable>disable</Nullable>
77
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
88
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>

samples/BasicSample/Program.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
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;
102
using System.Collections.Generic;
113
using System.ComponentModel.DataAnnotations.Schema;
12-
using System.Diagnostics;
134
using System.Linq;
5+
using EntityFrameworkCore.Projectables;
6+
using Microsoft.Data.Sqlite;
7+
using Microsoft.EntityFrameworkCore;
8+
using Microsoft.Extensions.DependencyInjection;
149

1510
namespace 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

Comments
 (0)