-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
88 lines (68 loc) · 2.27 KB
/
Copy pathProgram.cs
File metadata and controls
88 lines (68 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System.ComponentModel.DataAnnotations;
namespace EFInstanceMethodTest
{
internal class Program
{
private class Executor
{
private string ImAnInstanceMethod(string? s) => s ?? "";
private static string ImAStaticMethod(string? s) => s ?? "";
public void DoInstanceThing()
{
using (var dx = new TestContext())
{
var result = dx.SOME_TABLE.Select(t => ImAnInstanceMethod(t.Field)).FirstOrDefault();
Console.WriteLine(result);
}
}
public void DoStaticThing()
{
using (var dx = new TestContext())
{
var result = dx.SOME_TABLE.Select(t => ImAStaticMethod(t.Field)).FirstOrDefault();
Console.WriteLine(result);
}
}
}
static void Main(string[] args)
{
var exe = new Executor();
Console.WriteLine("Calling a static method from final projection. Result:");
RunAndDump(exe.DoStaticThing);
Console.WriteLine("\r\nCalling an instance method from final projection. Result:");
RunAndDump(exe.DoInstanceThing);
}
private static void RunAndDump(Action a)
{
try
{
a();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
internal class TestContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("<put your connection string here>", b => b.UseRelationalNulls(true));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("testing");
modelBuilder.Entity<SOME_TABLE>().HasNoKey();
}
public virtual DbSet<SOME_TABLE> SOME_TABLE { get; set; }
}
internal class SOME_TABLE
{
[StringLength(25)]
[Unicode(false)]
public string? Field { get; set; }
}
}