├── .gitignore
├── .vs
└── EFCore5Demos
│ └── DesignTimeBuild
│ └── .dtbcache.v2
├── DbContextDemo
├── Customers.cs
├── DbContextDemo.csproj
├── MyDBContext.cs
├── PeformanceDemo.cs
├── Products.cs
├── Program.cs
└── efpt.config.json
├── EF2QueryType
├── Customers.cs
├── EF2QueryType.csproj
├── MyDbContext.cs
├── Program.cs
└── efpt.config.json
├── EF5QueryType
├── Customers.cs
├── EF5QueryType.csproj
├── MyDBContext.cs
├── Program.cs
└── efpt.config.json
├── EFCore5Demos.csproj
├── EFCore5Demos.sln
├── ManyToManyMapping
├── ManyToManyMapping.csproj
├── MyDBContext.cs
└── Program.cs
├── MapUserFunction
├── MapUserFunction.csproj
├── MyDBContext.cs
└── Program.cs
├── Program.cs
├── PropertyBagDemo
├── DynamicQuery1.cs
├── DynamicQuery2.cs
├── MyDbContext.cs
├── Program.cs
└── PropertyBagDemo.csproj
├── RelationsIncludeFilter
├── MyDBContext.cs
├── OrderDetails.cs
├── Orders.cs
├── Program.cs
├── RelationsIncludeFilter.csproj
└── efpt.config.json
├── SoftDeleteDemo
├── MyDBContext.cs
├── Products.cs
├── Program.cs
├── SoftDeleteDemo.csproj
└── efpt.config.json
├── UpdateQueryMapping
├── Customers.cs
├── MyDBContext.cs
├── Program.cs
├── UpdateQueryMapping.csproj
└── efpt.config.json
├── UseChangeProxies
├── Customers.cs
├── MyDBContext.cs
├── Program.cs
├── UseChangeProxies.csproj
└── efpt.config.json
├── UseComputeSql
├── MyDBContext.cs
├── Orders.cs
├── Program.cs
├── UseComputeSql.csproj
└── efpt.config.json
├── UseDbContextFactory
├── Customers.cs
├── IMyService1.cs
├── MyDBContext.cs
├── MyService1.cs
├── Program.cs
├── UseDbContextFactory.csproj
└── efpt.config.json
├── UseSaveChangesInterceptor
├── Customers.cs
├── MyDBContext.cs
├── Program.cs
├── UseSaveChangesInterceptor.csproj
└── efpt.config.json
├── UseSavepoint
├── Customers.cs
├── MyDBContext.cs
├── Program.cs
├── UseSavepoint.csproj
└── efpt.config.json
├── UseSavingChangesEvent
├── Customers.cs
├── MyDBContext.cs
├── Program.cs
├── UseSavingChangesEvent.csproj
└── efpt.config.json
├── UseSplitQuery
├── MyDBContext.cs
├── Program.cs
└── UseSplitQuery.csproj
├── UseTPH
├── MyDBContext.cs
├── Program.cs
└── UseTPH.csproj
├── UseTPT
├── MyDBContext.cs
├── Program.cs
└── UseTPT.csproj
└── UseTVF
├── MyDBContext.cs
├── Program.cs
├── UseTVF.csproj
└── tvf_backup.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.sln.docstates
8 |
9 | # Build results
10 |
11 | [Dd]ebug/
12 | [Rr]elease/
13 | x64/
14 | build/
15 | [Bb]in/
16 | [Oo]bj/
17 |
18 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
19 | !packages/*/build/
20 |
21 | # MSTest test Results
22 | [Tt]est[Rr]esult*/
23 | [Bb]uild[Ll]og.*
24 |
25 | *_i.c
26 | *_p.c
27 | *.ilk
28 | *.meta
29 | *.obj
30 | *.pch
31 | *.pdb
32 | *.pgc
33 | *.pgd
34 | *.rsp
35 | *.sbr
36 | *.tlb
37 | *.tli
38 | *.tlh
39 | *.tmp
40 | *.tmp_proj
41 | *.log
42 | *.vspscc
43 | *.vssscc
44 | .builds
45 | *.pidb
46 | *.log
47 | *.scc
48 |
49 | # Visual C++ cache files
50 | ipch/
51 | *.aps
52 | *.ncb
53 | *.opensdf
54 | *.sdf
55 | *.cachefile
56 |
57 | # Visual Studio profiler
58 | *.psess
59 | *.vsp
60 | *.vspx
61 |
62 | # Guidance Automation Toolkit
63 | *.gpState
64 |
65 | # ReSharper is a .NET coding add-in
66 | _ReSharper*/
67 | *.[Rr]e[Ss]harper
68 |
69 | # TeamCity is a build add-in
70 | _TeamCity*
71 |
72 | # DotCover is a Code Coverage Tool
73 | *.dotCover
74 |
75 | # NCrunch
76 | *.ncrunch*
77 | .*crunch*.local.xml
78 |
79 | # Installshield output folder
80 | [Ee]xpress/
81 |
82 | # DocProject is a documentation generator add-in
83 | DocProject/buildhelp/
84 | DocProject/Help/*.HxT
85 | DocProject/Help/*.HxC
86 | DocProject/Help/*.hhc
87 | DocProject/Help/*.hhk
88 | DocProject/Help/*.hhp
89 | DocProject/Help/Html2
90 | DocProject/Help/html
91 |
92 | # Click-Once directory
93 | publish/
94 |
95 | # Publish Web Output
96 | *.Publish.xml
97 |
98 | # NuGet Packages Directory
99 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line
100 | #packages/
101 |
102 | # Windows Azure Build Output
103 | csx
104 | *.build.csdef
105 |
106 | # Windows Store app package directory
107 | AppPackages/
108 |
109 | # Others
110 | sql/
111 | *.Cache
112 | ClientBin/
113 | [Ss]tyle[Cc]op.*
114 | ~$*
115 | *~
116 | *.dbmdl
117 | *.[Pp]ublish.xml
118 | *.pfx
119 | *.publishsettings
120 |
121 | # RIA/Silverlight projects
122 | Generated_Code/
123 |
124 | # Backup & report files from converting an old project file to a newer
125 | # Visual Studio version. Backup files are not needed, because we have git ;-)
126 | _UpgradeReport_Files/
127 | Backup*/
128 | UpgradeLog*.XML
129 | UpgradeLog*.htm
130 |
131 | # SQL Server files
132 | App_Data/*.mdf
133 | App_Data/*.ldf
134 |
135 |
136 | #LightSwitch generated files
137 | GeneratedArtifacts/
138 | _Pvt_Extensions/
139 | ModelManifest.xml
140 |
141 | # =========================
142 | # Windows detritus
143 | # =========================
144 |
145 | # Windows image file caches
146 | Thumbs.db
147 | ehthumbs.db
148 |
149 | # Folder config file
150 | Desktop.ini
151 |
152 | # Recycle Bin used on file shares
153 | $RECYCLE.BIN/
154 |
155 | # Mac desktop service store files
156 | .DS_Store
157 |
--------------------------------------------------------------------------------
/.vs/EFCore5Demos/DesignTimeBuild/.dtbcache.v2:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/code6421vs/EF5Demo/9cc448ee6aa98fbd99fd05cdb6acc91f4a17ff90/.vs/EFCore5Demos/DesignTimeBuild/.dtbcache.v2
--------------------------------------------------------------------------------
/DbContextDemo/Customers.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace DbContextDemo
8 | {
9 | public partial class Customers
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public string Address { get; set; }
14 | public int CreditLevel { get; set; }
15 | }
16 | }
--------------------------------------------------------------------------------
/DbContextDemo/DbContextDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/DbContextDemo/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace DbContextDemo
9 | {
10 | public partial class MyDBContext : DbContext
11 | {
12 | public MyDBContext()
13 | {
14 | }
15 |
16 | public MyDBContext(DbContextOptions options)
17 | : base(options)
18 | {
19 | }
20 |
21 | public virtual DbSet Customers { get; set; }
22 | public virtual DbSet Products { get; set; }
23 |
24 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
25 | {
26 | optionsBuilder.UseSqlServer(
27 | @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB2;Integrated Security=True;Connect Timeout=30");
28 | base.OnConfiguring(optionsBuilder);
29 | }
30 |
31 | protected override void OnModelCreating(ModelBuilder modelBuilder)
32 | {
33 | modelBuilder.Entity(entity =>
34 | {
35 | entity.Property(e => e.Address)
36 | .HasMaxLength(250)
37 | .HasColumnName("ADDRESS");
38 |
39 | entity.Property(e => e.CreditLevel).HasColumnName("CREDIT_LEVEL");
40 |
41 | entity.Property(e => e.Name)
42 | .HasMaxLength(120)
43 | .HasColumnName("NAME");
44 | });
45 |
46 | modelBuilder.Entity(entity =>
47 | {
48 | entity.Property(e => e.Id).ValueGeneratedNever();
49 |
50 | entity.Property(e => e.Name).HasMaxLength(60);
51 |
52 | entity.Property(e => e.Price).HasColumnType("decimal(18, 0)");
53 | });
54 |
55 | OnModelCreatingPartial(modelBuilder);
56 | }
57 |
58 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
59 | }
60 | }
--------------------------------------------------------------------------------
/DbContextDemo/PeformanceDemo.cs:
--------------------------------------------------------------------------------
1 | using Dapper;
2 | using Microsoft.Data.SqlClient;
3 | using Microsoft.EntityFrameworkCore;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Diagnostics;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace DbContextDemo
12 | {
13 | public static class PeformanceDemo
14 | {
15 | static void GenerateCustomersV3()
16 | {
17 | var ctx = new MyDBContext();
18 | const string characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
19 | var random = new Random();
20 | var data = new List();
21 | for (int i = 0; i < 10000; i++)
22 | {
23 | data.Add(new Customers()
24 | {
25 | Name = new string(Enumerable.Repeat(characters, 10).Select(a => a[random.Next(a.Length)]).ToArray()),
26 | Address = new string(Enumerable.Repeat(characters, 10).Select(a => a[random.Next(a.Length)]).ToArray())
27 | });
28 | }
29 | ctx.Customers.AddRange(data);
30 | ctx.SaveChanges();
31 | }
32 |
33 | static void QueryEF()
34 | {
35 | var ctx = new MyDBContext();
36 | var data = ctx.Customers.Where(a => a.Name.Contains("c")).ToList();
37 | }
38 |
39 | static void QueryEF_NoTracking()
40 | {
41 | var ctx = new MyDBContext();
42 | var data = ctx.Customers.AsNoTracking().Where(a => a.Name.Contains("c")).ToList();
43 | }
44 |
45 | static void QueryDapper()
46 | {
47 | using var conn = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB2;Integrated Security=True;Connect Timeout=30");
48 | var data = conn.Query("SELECT * FROM Customers Where NAME Like '%c'").ToList(); ;
49 | }
50 |
51 | static void Benchmark(Action f, string label)
52 | {
53 | var sw = new Stopwatch();
54 | sw.Start();
55 | f();
56 | sw.Stop();
57 | Console.WriteLine($"{label} elapsed {sw.ElapsedMilliseconds} ms");
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/DbContextDemo/Products.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace DbContextDemo
8 | {
9 | public partial class Products
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public decimal? Price { get; set; }
14 | public bool? IsDeleted { get; set; }
15 | }
16 | }
--------------------------------------------------------------------------------
/DbContextDemo/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Data.SqlClient;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Diagnostics;
5 | using System.Linq;
6 | using Dapper;
7 | using Microsoft.EntityFrameworkCore;
8 |
9 | namespace DbContextDemo
10 | {
11 | class Program
12 | {
13 | static void ContextLifetime()
14 | {
15 | var ctx = new MyDBContext();
16 | for(var i = 0; i < 100; i++)
17 | {
18 | var c = new Customers
19 | {
20 | Name = $"code6421{i}",
21 | Address = "Taipei"
22 | };
23 | ctx.Customers.Add(c);
24 | }
25 |
26 | ctx.SaveChanges();
27 |
28 | //now dbcontext has 100 entities
29 | var c1 = new Customers
30 | {
31 | Name = $"code6421-{DateTime.Now.ToFileTime()}"
32 | };
33 | ctx.Customers.Add(c1);
34 | //below save changes will compare 100+1 to tracking for update
35 | ctx.SaveChanges();
36 | }
37 |
38 | static void ContextLifetime2()
39 | {
40 | var ctx = new MyDBContext();
41 | for (var i = 0; i < 3; i++)
42 | {
43 | var c = new Customers
44 | {
45 | Name = $"code6421{i}",
46 | Address = "Taipei"
47 | };
48 | ctx.Customers.Add(c);
49 | }
50 |
51 | ctx.SaveChanges();
52 | Console.WriteLine($"before clean tracking {ctx.ChangeTracker.Entries().Count()}");
53 | ctx.ChangeTracker.Clear(); //clean all saved entities
54 |
55 | //now dbcontext is clean
56 | Console.WriteLine($"after clean tracking {ctx.ChangeTracker.Entries().Count()}");
57 | }
58 |
59 | static void Main(string[] args)
60 | {
61 | //for (int i = 0; i < 5; i++)
62 | //{
63 | // Benchmark(QueryEF, "EF");
64 | // Benchmark(QueryEF_NoTracking, "EF(No Tracking)");
65 | // Benchmark(QueryDapper, "Dapper");
66 | //}
67 | ContextLifetime2();
68 |
69 |
70 | Console.ReadLine();
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/DbContextDemo/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "Myefc_DB2Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": false,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "DbContextDemo",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Customers]",
18 | "ObjectType": 0
19 | },
20 | {
21 | "HasPrimaryKey": true,
22 | "Name": "[dbo].[Products]",
23 | "ObjectType": 0
24 | }
25 | ],
26 | "UseDatabaseNames": false,
27 | "UseDbContextSplitting": false,
28 | "UseFluentApiOnly": true,
29 | "UseHandleBars": false,
30 | "UseInflector": false,
31 | "UseLegacyPluralizer": false,
32 | "UseNodaTime": false,
33 | "UseSpatial": false
34 | }
--------------------------------------------------------------------------------
/EF2QueryType/Customers.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace EF2QueryType
8 | {
9 | public partial class Customers
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public string Address { get; set; }
14 | public int CreditLevel { get; set; }
15 | }
16 | }
--------------------------------------------------------------------------------
/EF2QueryType/EF2QueryType.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/EF2QueryType/MyDbContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace EF2QueryType
9 | {
10 | public partial class MyDbContext : DbContext
11 | {
12 | public MyDbContext()
13 | {
14 | }
15 |
16 | public MyDbContext(DbContextOptions options)
17 | : base(options)
18 | {
19 | }
20 |
21 | public virtual DbQuery Customers { get; set; }
22 |
23 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
24 | {
25 | optionsBuilder.UseSqlServer(
26 | @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB2;Integrated Security=True;Connect Timeout=30");
27 | base.OnConfiguring(optionsBuilder);
28 | }
29 |
30 | protected override void OnModelCreating(ModelBuilder modelBuilder)
31 | {
32 | modelBuilder.Entity(entity =>
33 | {
34 | entity.Property(e => e.Address)
35 | .HasMaxLength(250)
36 | .HasColumnName("ADDRESS");
37 |
38 | entity.Property(e => e.CreditLevel).HasColumnName("CREDIT_LEVEL");
39 |
40 | entity.Property(e => e.Name)
41 | .HasMaxLength(120)
42 | .HasColumnName("NAME");
43 | });
44 |
45 | OnModelCreatingPartial(modelBuilder);
46 | }
47 |
48 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
49 | }
50 | }
--------------------------------------------------------------------------------
/EF2QueryType/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace EF2QueryType
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | Console.WriteLine("Hello World!");
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/EF2QueryType/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "Myefc_DB2Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": false,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "EF2QueryType",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Customers]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/EF5QueryType/Customers.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace EF5QueryType
8 | {
9 | public partial class Customers
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public string Address { get; set; }
14 | }
15 | }
--------------------------------------------------------------------------------
/EF5QueryType/EF5QueryType.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/EF5QueryType/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace EF5QueryType
9 | {
10 | public partial class MyDBContext : DbContext
11 | {
12 | public MyDBContext()
13 | {
14 | }
15 |
16 | public MyDBContext(DbContextOptions options)
17 | : base(options)
18 | {
19 | }
20 |
21 | public virtual DbSet Customers { get; set; }
22 |
23 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
24 | {
25 | if (!optionsBuilder.IsConfigured)
26 | {
27 | #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.
28 | optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyEFC_DB2;Integrated Security=True");
29 | optionsBuilder.LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information);
30 | }
31 | }
32 |
33 | protected override void OnModelCreating(ModelBuilder modelBuilder)
34 | {
35 | modelBuilder.Entity().ToSqlQuery("SELECT * FROM Customers -- i am custom sql");
36 |
37 | OnModelCreatingPartial(modelBuilder);
38 | }
39 |
40 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
41 | }
42 | }
--------------------------------------------------------------------------------
/EF5QueryType/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace EF5QueryType
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var ctx = new MyDBContext();
11 | var r = ctx.Customers.Where(a => a.Name.Contains("code6421")).ToList();
12 | Console.ReadLine();
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/EF5QueryType/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "Myefc_DB2Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": true,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "EF5QueryType",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Customers]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/EFCore5Demos.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/EFCore5Demos.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.30717.126
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftDeleteDemo", "SoftDeleteDemo\SoftDeleteDemo.csproj", "{C7F03FCE-4EB9-4F25-9347-7EF5C19B3C6F}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseComputeSql", "UseComputeSql\UseComputeSql.csproj", "{06FE456C-619D-4305-BCBB-A297A9BBAE7C}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseSplitQuery", "UseSplitQuery\UseSplitQuery.csproj", "{1861C32C-3FE9-4304-8B11-11CFDFB1EEDD}"
11 | EndProject
12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbContextDemo", "DbContextDemo\DbContextDemo.csproj", "{FC3354F6-11FE-4CBC-B36D-D98D38E2CF42}"
13 | EndProject
14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseChangeProxies", "UseChangeProxies\UseChangeProxies.csproj", "{ED7C3E8B-AA49-46AA-BF0A-BEC0EB4EE32A}"
15 | EndProject
16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseSavepoint", "UseSavepoint\UseSavepoint.csproj", "{F4B47E68-C68A-48FE-8BA3-2E737FF936D6}"
17 | EndProject
18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EF5QueryType", "EF5QueryType\EF5QueryType.csproj", "{7AC90E7A-D956-4869-A870-F2A276E39A3D}"
19 | EndProject
20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UpdateQueryMapping", "UpdateQueryMapping\UpdateQueryMapping.csproj", "{2BC17489-B782-4BBF-A4A8-82C376EE7A8A}"
21 | EndProject
22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RelationsIncludeFilter", "RelationsIncludeFilter\RelationsIncludeFilter.csproj", "{A0601B4A-5572-4C5D-BE13-2F8BE2D57179}"
23 | EndProject
24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseTPH", "UseTPH\UseTPH.csproj", "{8FBA9F3C-349A-4A30-8416-09867FD0F779}"
25 | EndProject
26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseTPT", "UseTPT\UseTPT.csproj", "{29A88939-49B1-4F5E-92ED-370B98A1C4E3}"
27 | EndProject
28 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseSavingChangesEvent", "UseSavingChangesEvent\UseSavingChangesEvent.csproj", "{1474D694-3AB3-4310-B70C-8F9E90F3CF04}"
29 | EndProject
30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseSaveChangesInterceptor", "UseSaveChangesInterceptor\UseSaveChangesInterceptor.csproj", "{61F1D289-5252-48ED-A890-351A0B57D1AE}"
31 | EndProject
32 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ManyToManyMapping", "ManyToManyMapping\ManyToManyMapping.csproj", "{2BEE2FA6-D300-44F5-ABAC-23047F48F0B3}"
33 | EndProject
34 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseDbContextFactory", "UseDbContextFactory\UseDbContextFactory.csproj", "{D92012DE-BC62-496B-BC25-708A73319A69}"
35 | EndProject
36 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UseTVF", "UseTVF\UseTVF.csproj", "{E139BB94-D116-497D-9F11-E3E9F195D8BC}"
37 | EndProject
38 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MapUserFunction", "MapUserFunction\MapUserFunction.csproj", "{30BD8E29-8673-4899-AC63-53FC5A3D41B9}"
39 | EndProject
40 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PropertyBagDemo", "PropertyBagDemo\PropertyBagDemo.csproj", "{E185C3E1-06D5-49F0-997C-A6EAA97F792F}"
41 | EndProject
42 | Global
43 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
44 | Debug|Any CPU = Debug|Any CPU
45 | Release|Any CPU = Release|Any CPU
46 | EndGlobalSection
47 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
48 | {C7F03FCE-4EB9-4F25-9347-7EF5C19B3C6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49 | {C7F03FCE-4EB9-4F25-9347-7EF5C19B3C6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
50 | {C7F03FCE-4EB9-4F25-9347-7EF5C19B3C6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
51 | {C7F03FCE-4EB9-4F25-9347-7EF5C19B3C6F}.Release|Any CPU.Build.0 = Release|Any CPU
52 | {06FE456C-619D-4305-BCBB-A297A9BBAE7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
53 | {06FE456C-619D-4305-BCBB-A297A9BBAE7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
54 | {06FE456C-619D-4305-BCBB-A297A9BBAE7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
55 | {06FE456C-619D-4305-BCBB-A297A9BBAE7C}.Release|Any CPU.Build.0 = Release|Any CPU
56 | {1861C32C-3FE9-4304-8B11-11CFDFB1EEDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57 | {1861C32C-3FE9-4304-8B11-11CFDFB1EEDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
58 | {1861C32C-3FE9-4304-8B11-11CFDFB1EEDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
59 | {1861C32C-3FE9-4304-8B11-11CFDFB1EEDD}.Release|Any CPU.Build.0 = Release|Any CPU
60 | {FC3354F6-11FE-4CBC-B36D-D98D38E2CF42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61 | {FC3354F6-11FE-4CBC-B36D-D98D38E2CF42}.Debug|Any CPU.Build.0 = Debug|Any CPU
62 | {FC3354F6-11FE-4CBC-B36D-D98D38E2CF42}.Release|Any CPU.ActiveCfg = Release|Any CPU
63 | {FC3354F6-11FE-4CBC-B36D-D98D38E2CF42}.Release|Any CPU.Build.0 = Release|Any CPU
64 | {ED7C3E8B-AA49-46AA-BF0A-BEC0EB4EE32A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65 | {ED7C3E8B-AA49-46AA-BF0A-BEC0EB4EE32A}.Debug|Any CPU.Build.0 = Debug|Any CPU
66 | {ED7C3E8B-AA49-46AA-BF0A-BEC0EB4EE32A}.Release|Any CPU.ActiveCfg = Release|Any CPU
67 | {ED7C3E8B-AA49-46AA-BF0A-BEC0EB4EE32A}.Release|Any CPU.Build.0 = Release|Any CPU
68 | {F4B47E68-C68A-48FE-8BA3-2E737FF936D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69 | {F4B47E68-C68A-48FE-8BA3-2E737FF936D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
70 | {F4B47E68-C68A-48FE-8BA3-2E737FF936D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
71 | {F4B47E68-C68A-48FE-8BA3-2E737FF936D6}.Release|Any CPU.Build.0 = Release|Any CPU
72 | {7AC90E7A-D956-4869-A870-F2A276E39A3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
73 | {7AC90E7A-D956-4869-A870-F2A276E39A3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
74 | {7AC90E7A-D956-4869-A870-F2A276E39A3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
75 | {7AC90E7A-D956-4869-A870-F2A276E39A3D}.Release|Any CPU.Build.0 = Release|Any CPU
76 | {2BC17489-B782-4BBF-A4A8-82C376EE7A8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
77 | {2BC17489-B782-4BBF-A4A8-82C376EE7A8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
78 | {2BC17489-B782-4BBF-A4A8-82C376EE7A8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
79 | {2BC17489-B782-4BBF-A4A8-82C376EE7A8A}.Release|Any CPU.Build.0 = Release|Any CPU
80 | {A0601B4A-5572-4C5D-BE13-2F8BE2D57179}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
81 | {A0601B4A-5572-4C5D-BE13-2F8BE2D57179}.Debug|Any CPU.Build.0 = Debug|Any CPU
82 | {A0601B4A-5572-4C5D-BE13-2F8BE2D57179}.Release|Any CPU.ActiveCfg = Release|Any CPU
83 | {A0601B4A-5572-4C5D-BE13-2F8BE2D57179}.Release|Any CPU.Build.0 = Release|Any CPU
84 | {8FBA9F3C-349A-4A30-8416-09867FD0F779}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
85 | {8FBA9F3C-349A-4A30-8416-09867FD0F779}.Debug|Any CPU.Build.0 = Debug|Any CPU
86 | {8FBA9F3C-349A-4A30-8416-09867FD0F779}.Release|Any CPU.ActiveCfg = Release|Any CPU
87 | {8FBA9F3C-349A-4A30-8416-09867FD0F779}.Release|Any CPU.Build.0 = Release|Any CPU
88 | {29A88939-49B1-4F5E-92ED-370B98A1C4E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
89 | {29A88939-49B1-4F5E-92ED-370B98A1C4E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
90 | {29A88939-49B1-4F5E-92ED-370B98A1C4E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
91 | {29A88939-49B1-4F5E-92ED-370B98A1C4E3}.Release|Any CPU.Build.0 = Release|Any CPU
92 | {1474D694-3AB3-4310-B70C-8F9E90F3CF04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
93 | {1474D694-3AB3-4310-B70C-8F9E90F3CF04}.Debug|Any CPU.Build.0 = Debug|Any CPU
94 | {1474D694-3AB3-4310-B70C-8F9E90F3CF04}.Release|Any CPU.ActiveCfg = Release|Any CPU
95 | {1474D694-3AB3-4310-B70C-8F9E90F3CF04}.Release|Any CPU.Build.0 = Release|Any CPU
96 | {61F1D289-5252-48ED-A890-351A0B57D1AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
97 | {61F1D289-5252-48ED-A890-351A0B57D1AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
98 | {61F1D289-5252-48ED-A890-351A0B57D1AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
99 | {61F1D289-5252-48ED-A890-351A0B57D1AE}.Release|Any CPU.Build.0 = Release|Any CPU
100 | {2BEE2FA6-D300-44F5-ABAC-23047F48F0B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
101 | {2BEE2FA6-D300-44F5-ABAC-23047F48F0B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
102 | {2BEE2FA6-D300-44F5-ABAC-23047F48F0B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
103 | {2BEE2FA6-D300-44F5-ABAC-23047F48F0B3}.Release|Any CPU.Build.0 = Release|Any CPU
104 | {D92012DE-BC62-496B-BC25-708A73319A69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
105 | {D92012DE-BC62-496B-BC25-708A73319A69}.Debug|Any CPU.Build.0 = Debug|Any CPU
106 | {D92012DE-BC62-496B-BC25-708A73319A69}.Release|Any CPU.ActiveCfg = Release|Any CPU
107 | {D92012DE-BC62-496B-BC25-708A73319A69}.Release|Any CPU.Build.0 = Release|Any CPU
108 | {E139BB94-D116-497D-9F11-E3E9F195D8BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
109 | {E139BB94-D116-497D-9F11-E3E9F195D8BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
110 | {E139BB94-D116-497D-9F11-E3E9F195D8BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
111 | {E139BB94-D116-497D-9F11-E3E9F195D8BC}.Release|Any CPU.Build.0 = Release|Any CPU
112 | {30BD8E29-8673-4899-AC63-53FC5A3D41B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
113 | {30BD8E29-8673-4899-AC63-53FC5A3D41B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
114 | {30BD8E29-8673-4899-AC63-53FC5A3D41B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
115 | {30BD8E29-8673-4899-AC63-53FC5A3D41B9}.Release|Any CPU.Build.0 = Release|Any CPU
116 | {E185C3E1-06D5-49F0-997C-A6EAA97F792F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
117 | {E185C3E1-06D5-49F0-997C-A6EAA97F792F}.Debug|Any CPU.Build.0 = Debug|Any CPU
118 | {E185C3E1-06D5-49F0-997C-A6EAA97F792F}.Release|Any CPU.ActiveCfg = Release|Any CPU
119 | {E185C3E1-06D5-49F0-997C-A6EAA97F792F}.Release|Any CPU.Build.0 = Release|Any CPU
120 | EndGlobalSection
121 | GlobalSection(SolutionProperties) = preSolution
122 | HideSolutionNode = FALSE
123 | EndGlobalSection
124 | GlobalSection(ExtensibilityGlobals) = postSolution
125 | SolutionGuid = {EF5BAF7D-0AEA-4C16-B1E4-82A3F5F20682}
126 | EndGlobalSection
127 | EndGlobal
128 |
--------------------------------------------------------------------------------
/ManyToManyMapping/ManyToManyMapping.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/ManyToManyMapping/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Text;
5 |
6 | namespace ManyToManyMapping
7 | {
8 | public class Doctors
9 | {
10 | public int Id { get; set; }
11 | public string Name { get; set; }
12 | public virtual ICollection Patients { get; set; }
13 |
14 | public Doctors()
15 | {
16 | Patients = new List();
17 | }
18 | }
19 |
20 | public class Patients
21 | {
22 | public int Id { get; set; }
23 | public string Name { get; set; }
24 | public virtual ICollection Doctors { get; set; }
25 |
26 | public Patients()
27 | {
28 | Doctors = new List();
29 | }
30 | }
31 |
32 | public partial class MyDBContext : DbContext
33 | {
34 | public MyDBContext()
35 | {
36 | }
37 |
38 | public MyDBContext(DbContextOptions options)
39 | : base(options)
40 | {
41 | }
42 |
43 | public virtual DbSet Doctors { get; set; }
44 | public virtual DbSet Patients { get; set; }
45 |
46 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
47 | {
48 | if (!optionsBuilder.IsConfigured)
49 | {
50 | #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.
51 | optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyEFC_DB7;Integrated Security=True");
52 | optionsBuilder.LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information);
53 | }
54 | }
55 |
56 | protected override void OnModelCreating(ModelBuilder modelBuilder)
57 | {
58 | //modelBuilder.Entity()
59 | // .HasMany(p => p.Patients)
60 | // .WithMany(p => p.Doctors)
61 | // .UsingEntity(
62 | // j => j.HasOne(pt => pt.Patient).WithMany().HasForeignKey(a => a.PatientsId),
63 | // j => j.HasOne(pt => pt.Doctor).WithMany().HasForeignKey(a => a.DoctorsId));
64 | OnModelCreatingPartial(modelBuilder);
65 | }
66 |
67 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
68 | }
69 |
70 | //public class DoctorsPatients
71 | //{
72 | // public int DoctorsId { get; set; }
73 | // public Doctors Doctor { get; set; }
74 | // public int PatientsId { get; set; }
75 | // public Patients Patient { get; set; }
76 | //}
77 | }
78 |
--------------------------------------------------------------------------------
/ManyToManyMapping/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 |
5 | namespace ManyToManyMapping
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | var ctx = new MyDBContext();
12 | ctx.Database.EnsureCreated();
13 | var d = new Doctors
14 | {
15 | Name = "jeff",
16 | Patients = new List()
17 | {
18 | new Patients { Name = "code6421"}
19 | }
20 | };
21 |
22 | ctx.Doctors.Add(d);
23 | ctx.SaveChanges();
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/MapUserFunction/MapUserFunction.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/MapUserFunction/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using Microsoft.Extensions.Logging;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.ComponentModel.DataAnnotations;
6 | using System.ComponentModel.DataAnnotations.Schema;
7 | using System.Configuration;
8 | using System.Text;
9 |
10 | namespace MapUserFunction
11 | {
12 | public class Customers
13 | {
14 | public int Id { get; set; }
15 | public string NAME { get; set; }
16 | public int CREDIT_LEVEL { get; set; }
17 | }
18 |
19 | public class MyDB2Context : DbContext
20 | {
21 |
22 | public virtual DbSet Customers { get; set; }
23 | // map to
24 | /* CREATE FUNCTION[dbo].[MySum]
25 | (
26 | @param1 int,
27 | @param2 int
28 | )
29 | RETURNS INT AS
30 | BEGIN
31 | RETURN @param1 + @param2
32 | END
33 | */
34 | [DbFunction("MySum")]
35 | public int MySum(int x, int y) => x + y;
36 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
37 | {
38 | optionsBuilder.UseSqlServer(@"Data Source=(LocalDB)\MSSQLLocalDB;Database=MyEFC_DB2;Integrated Security=True;Connect Timeout=30");
39 | optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information);
40 | base.OnConfiguring(optionsBuilder);
41 | }
42 |
43 | protected override void OnModelCreating(ModelBuilder modelBuilder)
44 | {
45 | base.OnModelCreating(modelBuilder);
46 | }
47 |
48 | public MyDB2Context() { }
49 |
50 | public MyDB2Context(DbContextOptions options) : base(options)
51 | {
52 |
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/MapUserFunction/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace MapUserFunction
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var ctx = new MyDB2Context();
11 | var result = (from s1 in ctx.Customers select ctx.MySum(s1.CREDIT_LEVEL, 12)).FirstOrDefault();
12 | Console.WriteLine(result);
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace EFCore5Demos
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | Console.WriteLine("Hello World!");
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/PropertyBagDemo/DynamicQuery1.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace PropertyBagDemo
8 | {
9 | public static class DynamicQuery1
10 | {
11 | static void QueryData2(string key, string value)
12 | {
13 | var ctx = new MyDbContext();
14 | var r = ctx.Customers.FirstOrDefault(a => a[key] == value);
15 | Console.WriteLine(r["NAME"]);
16 | }
17 |
18 | public static void Run()
19 | {
20 | QueryData2("NAME", "code6421");
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/PropertyBagDemo/DynamicQuery2.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace PropertyBagDemo
8 | {
9 | public static class DynamicQuery2
10 | {
11 | static void QueryData3(Dictionary query)
12 | {
13 | var ctx = new MyDbContext();
14 | if (query.Count == 0)
15 | throw new ArgumentException("query is empty");
16 | IQueryable> tempQuery = ctx.Customers;
17 | foreach (var item in query)
18 | {
19 | tempQuery = tempQuery.Where(a => a[item.Key] == query[item.Key]);
20 | }
21 |
22 | foreach (var item in tempQuery)
23 | {
24 | Console.WriteLine(item["NAME"]);
25 | }
26 | }
27 |
28 | public static void Run()
29 | {
30 | var d = new Dictionary
31 | {
32 | ["ADDRESS"] = "Taipei"
33 | };
34 |
35 | QueryData3(d);
36 | }
37 |
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/PropertyBagDemo/MyDbContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace PropertyBagDemo
9 | {
10 | public class MyDbContext : DbContext
11 | {
12 | public DbSet> Customers => Set>("Customers");
13 |
14 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
15 | {
16 | optionsBuilder.UseSqlServer(
17 | @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB2;Integrated Security=True;Connect Timeout=30");
18 | optionsBuilder.LogTo(Console.WriteLine);
19 | }
20 |
21 | protected override void OnModelCreating(ModelBuilder modelBuilder)
22 | {
23 | modelBuilder.SharedTypeEntity>("Customers", b =>
24 | {
25 | b.IndexerProperty("Id");
26 | b.IndexerProperty("NAME").IsRequired();
27 | b.IndexerProperty("ADDRESS");
28 | b.IndexerProperty("CREDIT_LEVEL");
29 | });
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/PropertyBagDemo/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Collections.Generic;
4 |
5 | namespace PropertyBagDemo
6 | {
7 | class Program
8 | {
9 | static void AddData()
10 | {
11 | var ctx = new MyDbContext();
12 | var rec1 = new Dictionary
13 | {
14 | ["NAME"] = "mary",
15 | ["ADDRESS"] = "Taipei"
16 | };
17 |
18 | ctx.Customers.Add(rec1);
19 | ctx.SaveChanges();
20 | }
21 |
22 | static void QueryData()
23 | {
24 | var ctx = new MyDbContext();
25 | var r = ctx.Customers.FirstOrDefault(a => a["NAME"] == "tom12");
26 | Console.WriteLine(r["NAME"]);
27 | }
28 |
29 |
30 | static void Main(string[] args)
31 | {
32 |
33 | Console.ReadLine();
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/PropertyBagDemo/PropertyBagDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/RelationsIncludeFilter/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace RelationsIncludeFilter
9 | {
10 | public partial class MyDBContext : DbContext
11 | {
12 | public MyDBContext()
13 | {
14 | }
15 |
16 | public MyDBContext(DbContextOptions options)
17 | : base(options)
18 | {
19 | }
20 |
21 | public virtual DbSet OrderDetails { get; set; }
22 | public virtual DbSet Orders { get; set; }
23 |
24 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
25 | {
26 | if (!optionsBuilder.IsConfigured)
27 | {
28 | #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.
29 | //optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MYEFC_DB5;Integrated Security=True;MultipleActiveResultSets=True");
30 | optionsBuilder.UseLazyLoadingProxies().UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MYEFC_DB5;Integrated Security=True;MultipleActiveResultSets=True");
31 | optionsBuilder.LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information);
32 | }
33 | }
34 |
35 | protected override void OnModelCreating(ModelBuilder modelBuilder)
36 | {
37 | modelBuilder.Entity(entity =>
38 | {
39 | entity.ToTable("Order_Details");
40 |
41 | entity.HasIndex(e => e.OrdersId, "IX_Order_Details_OrdersId");
42 |
43 | entity.Property(e => e.Price).HasColumnType("decimal(18, 2)");
44 |
45 | entity.HasOne(d => d.Orders)
46 | .WithMany(p => p.OrderDetails)
47 | .HasForeignKey(d => d.OrdersId);
48 | });
49 |
50 | OnModelCreatingPartial(modelBuilder);
51 | }
52 |
53 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
54 | }
55 | }
--------------------------------------------------------------------------------
/RelationsIncludeFilter/OrderDetails.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace RelationsIncludeFilter
8 | {
9 | public partial class OrderDetails
10 | {
11 | public int Id { get; set; }
12 | public string Product { get; set; }
13 | public int Qty { get; set; }
14 | public decimal Price { get; set; }
15 | public int? OrdersId { get; set; }
16 |
17 | public virtual Orders Orders { get; set; }
18 | }
19 | }
--------------------------------------------------------------------------------
/RelationsIncludeFilter/Orders.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace RelationsIncludeFilter
8 | {
9 | public partial class Orders
10 | {
11 | public Orders()
12 | {
13 | OrderDetails = new HashSet();
14 | }
15 |
16 | public int Id { get; set; }
17 | public DateTime OrderDate { get; set; }
18 | public string CustomerName { get; set; }
19 |
20 | public virtual ICollection OrderDetails { get; set; }
21 | }
22 | }
--------------------------------------------------------------------------------
/RelationsIncludeFilter/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using Microsoft.EntityFrameworkCore;
4 |
5 | namespace RelationsIncludeFilter
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | var ctx = new MyDBContext();
12 | foreach(var item in ctx.Orders)
13 | {
14 | //ctx.Entry(item).Collection(a => a.OrderDetails).Load();
15 | Console.WriteLine(item.OrderDetails.FirstOrDefault()?.Product);
16 | }
17 | //var r = ctx.Orders.Include(a => a.OrderDetails.Where(b => b.Product.Contains("Go"))).ToList();
18 | Console.ReadLine();
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/RelationsIncludeFilter/RelationsIncludeFilter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/RelationsIncludeFilter/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "MYEFC_DB5Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": true,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "RelationsIncludeFilter",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Order_Details]",
18 | "ObjectType": 0
19 | },
20 | {
21 | "HasPrimaryKey": true,
22 | "Name": "[dbo].[Orders]",
23 | "ObjectType": 0
24 | }
25 | ],
26 | "UseDatabaseNames": false,
27 | "UseDbContextSplitting": false,
28 | "UseFluentApiOnly": true,
29 | "UseHandleBars": false,
30 | "UseInflector": false,
31 | "UseLegacyPluralizer": false,
32 | "UseNodaTime": false,
33 | "UseSpatial": false
34 | }
--------------------------------------------------------------------------------
/SoftDeleteDemo/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 | using Microsoft.EntityFrameworkCore;
6 | using Microsoft.EntityFrameworkCore.Metadata;
7 |
8 | #nullable disable
9 |
10 | namespace SoftDeleteDemo
11 | {
12 | public partial class MyDBContext : DbContext
13 | {
14 | public MyDBContext()
15 | {
16 | }
17 |
18 | public MyDBContext(DbContextOptions options)
19 | : base(options)
20 | {
21 | }
22 |
23 | public virtual DbSet Products { get; set; }
24 |
25 | protected override void OnModelCreating(ModelBuilder modelBuilder)
26 | {
27 | modelBuilder.Entity(entity =>
28 | {
29 | entity.Property(e => e.Id).ValueGeneratedNever();
30 |
31 | entity.Property(e => e.Name).HasMaxLength(60);
32 |
33 | entity.Property(e => e.Price).HasColumnType("decimal(18, 0)");
34 | });
35 |
36 | modelBuilder.Entity().HasQueryFilter(m => EF.Property(m, "IsDeleted") == false);
37 |
38 | OnModelCreatingPartial(modelBuilder);
39 | }
40 |
41 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
42 | {
43 | optionsBuilder.UseSqlServer(
44 | @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB2;Integrated Security=True;Connect Timeout=30");
45 | base.OnConfiguring(optionsBuilder);
46 | }
47 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
48 |
49 |
50 | public override int SaveChanges()
51 | {
52 | UpdateSoftDeleteStatuses();
53 | return base.SaveChanges();
54 | }
55 |
56 | public override Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
57 | {
58 | UpdateSoftDeleteStatuses();
59 | return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
60 | }
61 |
62 | private void UpdateSoftDeleteStatuses()
63 | {
64 | foreach (var entry in ChangeTracker.Entries())
65 | {
66 | switch (entry.State)
67 | {
68 | case EntityState.Added:
69 | entry.CurrentValues["IsDeleted"] = false;
70 | break;
71 | case EntityState.Deleted:
72 | entry.State = EntityState.Modified;
73 | entry.CurrentValues["IsDeleted"] = true;
74 | break;
75 | }
76 | }
77 | }
78 | }
79 | }
--------------------------------------------------------------------------------
/SoftDeleteDemo/Products.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace SoftDeleteDemo
8 | {
9 | public partial class Products
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public decimal? Price { get; set; }
14 | public bool? IsDeleted { get; set; }
15 | }
16 | }
--------------------------------------------------------------------------------
/SoftDeleteDemo/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace SoftDeleteDemo
5 | {
6 | class Program
7 | {
8 | static void AddData()
9 | {
10 | var ctx = new MyDBContext();
11 | var c = new Products
12 | {
13 | Name = "C#",
14 | Price = 1200
15 | };
16 |
17 | var c1 = new Products
18 | {
19 | Name = "VBNET",
20 | Price = 1600
21 | };
22 |
23 | ctx.Products.Add(c);
24 | ctx.Products.Add(c1);
25 | ctx.SaveChanges();
26 | }
27 |
28 | static void QueryData()
29 | {
30 | var ctx = new MyDBContext();
31 | foreach (var item in ctx.Products)
32 | {
33 | Console.WriteLine($"{item.Name}");
34 | }
35 | }
36 |
37 | static void DeleteData()
38 | {
39 | var ctx = new MyDBContext();
40 | var r = ctx.Products.FirstOrDefault(a => a.Name == "C#");
41 | ctx.Products.Remove(r);
42 | ctx.SaveChanges();
43 | }
44 |
45 | static void Main(string[] args)
46 | {
47 | QueryData();
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/SoftDeleteDemo/SoftDeleteDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/SoftDeleteDemo/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "MyDBContext",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": false,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "SoftDeleteDemo",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Products]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/UpdateQueryMapping/Customers.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace UpdateQueryMapping
8 | {
9 | public partial class Customers
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public string Address { get; set; }
14 | }
15 | }
--------------------------------------------------------------------------------
/UpdateQueryMapping/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace UpdateQueryMapping
9 | {
10 | public partial class MyDBContext : DbContext
11 | {
12 | public MyDBContext()
13 | {
14 | }
15 |
16 | public MyDBContext(DbContextOptions options)
17 | : base(options)
18 | {
19 | }
20 |
21 | public virtual DbSet Customers { get; set; }
22 |
23 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
24 | {
25 | if (!optionsBuilder.IsConfigured)
26 | {
27 | #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.
28 | optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyEFC_DB2;Integrated Security=True");
29 | optionsBuilder.LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information);
30 | }
31 | }
32 |
33 | protected override void OnModelCreating(ModelBuilder modelBuilder)
34 | {
35 | modelBuilder.Entity().ToTable("Customers").ToView("Customers17View");
36 |
37 | OnModelCreatingPartial(modelBuilder);
38 | }
39 |
40 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
41 | }
42 | }
--------------------------------------------------------------------------------
/UpdateQueryMapping/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace UpdateQueryMapping
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var ctx = new MyDBContext();
11 | foreach(var item in ctx.Customers)
12 | {
13 | Console.WriteLine($"{item.Name}");
14 | }
15 |
16 | Console.WriteLine("press key to change data");
17 | Console.ReadLine();
18 | var r = ctx.Customers.FirstOrDefault();
19 | r.Name = $"code6421_modify_view{DateTime.Now.ToFileTime()}";
20 | ctx.SaveChanges();
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/UpdateQueryMapping/UpdateQueryMapping.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/UpdateQueryMapping/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "Myefc_DB2Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": true,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "UpdateQueryMapping",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Customers]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/UseChangeProxies/Customers.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 | using System.ComponentModel;
5 |
6 | #nullable disable
7 |
8 | namespace UseChangeProxies
9 | {
10 | public partial class Customers : INotifyPropertyChanging, INotifyPropertyChanged
11 | {
12 | private int id;
13 | private string name;
14 | private string address;
15 | private int creditLevel;
16 |
17 | public virtual int Id
18 | {
19 | get => id;
20 | set
21 | {
22 | id = value;
23 | OnPropertyChanged(nameof(Id));
24 | }
25 | }
26 | public virtual string Name
27 | {
28 | get => name;
29 | set
30 | {
31 | name = value;
32 | OnPropertyChanged(nameof(Name));
33 | }
34 | }
35 |
36 | public virtual string Address
37 | {
38 | get => address;
39 | set
40 | {
41 | address = value;
42 | OnPropertyChanged(nameof(Address));
43 | }
44 | }
45 |
46 | public virtual int CreditLevel
47 | {
48 | get => creditLevel;
49 | set
50 | {
51 | creditLevel = value;
52 | OnPropertyChanged(nameof(CreditLevel));
53 | }
54 | }
55 |
56 | private void OnPropertyChanging(string propName)
57 | {
58 | PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propName));
59 | }
60 |
61 | private void OnPropertyChanged(string propName)
62 | {
63 | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
64 | }
65 |
66 | public event PropertyChangingEventHandler PropertyChanging;
67 | public event PropertyChangedEventHandler PropertyChanged;
68 | }
69 | }
--------------------------------------------------------------------------------
/UseChangeProxies/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace UseChangeProxies
9 | {
10 | public partial class MyDBContext : DbContext
11 | {
12 | public MyDBContext()
13 | {
14 | }
15 |
16 | public MyDBContext(DbContextOptions options)
17 | : base(options)
18 | {
19 | }
20 |
21 | public virtual DbSet Customers { get; set; }
22 |
23 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
24 | {
25 | if (!optionsBuilder.IsConfigured)
26 | {
27 | #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.
28 | optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyEFC_DB2;Integrated Security=True");
29 | optionsBuilder.UseChangeTrackingProxies();
30 | }
31 | }
32 |
33 | protected override void OnModelCreating(ModelBuilder modelBuilder)
34 | {
35 | modelBuilder.Entity(entity =>
36 | {
37 | entity.Property(e => e.Address)
38 | .HasMaxLength(250)
39 | .HasColumnName("ADDRESS");
40 |
41 | entity.Property(e => e.CreditLevel).HasColumnName("CREDIT_LEVEL");
42 |
43 | entity.Property(e => e.Name)
44 | .HasMaxLength(120)
45 | .HasColumnName("NAME");
46 | });
47 |
48 | OnModelCreatingPartial(modelBuilder);
49 | }
50 |
51 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
52 | }
53 | }
--------------------------------------------------------------------------------
/UseChangeProxies/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using System;
3 | using System.Diagnostics;
4 | using System.Linq;
5 |
6 | namespace UseChangeProxies
7 | {
8 | class Program
9 | {
10 | static void AddData()
11 | {
12 | var ctx = new MyDBContext();
13 | for(var i = 0; i < 100; i++)
14 | {
15 | var o = new Customers
16 | {
17 | Name = $"code6421{i}",
18 | Address = "Taipei"
19 | };
20 | ctx.Customers.Add(o);
21 | }
22 | ctx.SaveChanges();
23 | }
24 |
25 | static void ChangeData()
26 | {
27 | var ctx = new MyDBContext();
28 | foreach(var item in ctx.Customers)
29 | {
30 | item.Name = $"{item.Name}-{DateTime.Now.ToFileTime()}";
31 | }
32 | ctx.SaveChanges();
33 | }
34 |
35 | static void Main(string[] args)
36 | {
37 | ChangeData();
38 | Console.ReadLine();
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/UseChangeProxies/UseChangeProxies.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/UseChangeProxies/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "Myefc_DB2Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": true,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "UseChangeProxies",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Customers]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/UseComputeSql/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace UseComputeSql
9 | {
10 | public partial class MyDBContext : DbContext
11 | {
12 | public MyDBContext()
13 | {
14 | }
15 |
16 | public MyDBContext(DbContextOptions options)
17 | : base(options)
18 | {
19 | }
20 |
21 | public virtual DbSet Orders { get; set; }
22 |
23 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
24 | {
25 | optionsBuilder.UseSqlServer(
26 | @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB4;Integrated Security=True;Connect Timeout=30");
27 | base.OnConfiguring(optionsBuilder);
28 | }
29 |
30 | protected override void OnModelCreating(ModelBuilder modelBuilder)
31 | {
32 | modelBuilder.Entity(entity =>
33 | {
34 | entity.Property(e => e.Id).ValueGeneratedNever();
35 |
36 | entity.Property(e => e.ProductName).HasMaxLength(50);
37 |
38 | entity.Property(e => e.Total).HasComputedColumnSql("Price * Qty", true);
39 | });
40 |
41 | OnModelCreatingPartial(modelBuilder);
42 | }
43 |
44 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
45 | }
46 | }
--------------------------------------------------------------------------------
/UseComputeSql/Orders.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace UseComputeSql
8 | {
9 | public partial class Orders
10 | {
11 | public int Id { get; set; }
12 | public string ProductName { get; set; }
13 | public decimal? Price { get; set; }
14 | public decimal? Qty { get; set; }
15 | public decimal? Total { get; set; }
16 | }
17 | }
--------------------------------------------------------------------------------
/UseComputeSql/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace UseComputeSql
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | var ctx = new MyDBContext();
10 | var o = new Orders
11 | {
12 | ProductName = "C#",
13 | Price = 120,
14 | Qty = 16
15 | };
16 | ctx.Orders.Add(o);
17 | ctx.SaveChanges();
18 | Console.WriteLine(o.Total);
19 | Console.ReadLine();
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/UseComputeSql/UseComputeSql.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/UseComputeSql/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "MyDBContext",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": false,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "UseComputeSql",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Orders]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/UseDbContextFactory/Customers.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace UseDbContextFactory
8 | {
9 | public partial class Customers
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public string Address { get; set; }
14 | public int CreditLevel { get; set; }
15 | }
16 | }
--------------------------------------------------------------------------------
/UseDbContextFactory/IMyService1.cs:
--------------------------------------------------------------------------------
1 | namespace UseDbContextFactory
2 | {
3 | public interface IMyService1
4 | {
5 | void WorkIt();
6 | }
7 | }
--------------------------------------------------------------------------------
/UseDbContextFactory/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace UseDbContextFactory
9 | {
10 | public partial class MyDBContext : DbContext
11 | {
12 | public MyDBContext()
13 | {
14 | }
15 |
16 | public virtual DbSet Customers { get; set; }
17 |
18 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
19 | {
20 | if (!optionsBuilder.IsConfigured)
21 | {
22 | #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.
23 | optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyEFC_DB2;Integrated Security=True");
24 | }
25 | }
26 |
27 | protected override void OnModelCreating(ModelBuilder modelBuilder)
28 | {
29 | modelBuilder.Entity(entity =>
30 | {
31 | entity.Property(e => e.Address)
32 | .HasMaxLength(250)
33 | .HasColumnName("ADDRESS");
34 |
35 | entity.Property(e => e.CreditLevel).HasColumnName("CREDIT_LEVEL");
36 |
37 | entity.Property(e => e.Name)
38 | .HasMaxLength(120)
39 | .HasColumnName("NAME");
40 | });
41 |
42 | OnModelCreatingPartial(modelBuilder);
43 | }
44 |
45 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
46 | }
47 | }
--------------------------------------------------------------------------------
/UseDbContextFactory/MyService1.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using System;
3 | using System.Linq;
4 | using System.Collections.Generic;
5 | using System.Text;
6 |
7 | namespace UseDbContextFactory
8 | {
9 | public class MyService1 : IMyService1
10 | {
11 | private IDbContextFactory contextFactory;
12 |
13 | public MyService1(IDbContextFactory factory)
14 | {
15 | contextFactory = factory;
16 | }
17 |
18 | public void WorkIt()
19 | {
20 | using var ctx = contextFactory.CreateDbContext();
21 | foreach (var item in ctx.Customers.Take(10))
22 | {
23 | Console.WriteLine($"Name: {item.Name}");
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/UseDbContextFactory/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.DependencyInjection;
2 | using System;
3 |
4 | namespace UseDbContextFactory
5 | {
6 | class Program
7 | {
8 | static IServiceProvider provider;
9 |
10 | static void Setup()
11 | {
12 | var sc = new ServiceCollection();
13 | sc.AddDbContextFactory();
14 | sc.AddTransient();
15 | provider = sc.BuildServiceProvider();
16 | }
17 |
18 | static void Main(string[] args)
19 | {
20 | Setup();
21 | var service = provider.GetRequiredService();
22 | service.WorkIt();
23 | Console.Read();
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/UseDbContextFactory/UseDbContextFactory.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/UseDbContextFactory/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "Myefc_DB2Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": true,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "UseDbContextFactory",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Customers]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/UseSaveChangesInterceptor/Customers.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace UseSaveChangesInterceptor
8 | {
9 | public partial class Customers
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public string Address { get; set; }
14 | public int CreditLevel { get; set; }
15 | }
16 | }
--------------------------------------------------------------------------------
/UseSaveChangesInterceptor/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Diagnostics;
5 | using Microsoft.EntityFrameworkCore.Metadata;
6 |
7 | #nullable disable
8 |
9 | namespace UseSaveChangesInterceptor
10 | {
11 | public partial class MyDBContext : DbContext
12 | {
13 | public MyDBContext()
14 | {
15 | }
16 |
17 | public MyDBContext(DbContextOptions options)
18 | : base(options)
19 | {
20 | }
21 |
22 | public virtual DbSet Customers { get; set; }
23 |
24 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
25 | {
26 | if (!optionsBuilder.IsConfigured)
27 | {
28 | #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.
29 | optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyEFC_DB2;Integrated Security=True");
30 | optionsBuilder.AddInterceptors(new MySaveChangesInterceptor());
31 | }
32 | }
33 |
34 | protected override void OnModelCreating(ModelBuilder modelBuilder)
35 | {
36 | modelBuilder.Entity(entity =>
37 | {
38 | entity.Property(e => e.Address)
39 | .HasMaxLength(250)
40 | .HasColumnName("ADDRESS");
41 |
42 | entity.Property(e => e.CreditLevel).HasColumnName("CREDIT_LEVEL");
43 |
44 | entity.Property(e => e.Name)
45 | .HasMaxLength(120)
46 | .HasColumnName("NAME");
47 | });
48 |
49 | OnModelCreatingPartial(modelBuilder);
50 | }
51 |
52 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
53 | }
54 |
55 | public class MySaveChangesInterceptor: SaveChangesInterceptor
56 | {
57 | public override InterceptionResult SavingChanges(DbContextEventData eventData, InterceptionResult result)
58 | {
59 | Console.WriteLine("before save");
60 | foreach (var item in eventData.Context.ChangeTracker.Entries())
61 | {
62 | if (item.State == Microsoft.EntityFrameworkCore.EntityState.Added && item.Entity is Customers)
63 | {
64 | var c = (Customers)item.Entity;
65 | if (string.IsNullOrEmpty(c.Address))
66 | throw new ArgumentException("Address must have value");
67 | }
68 | }
69 | return base.SavingChanges(eventData, result);
70 | }
71 |
72 | public override int SavedChanges(SaveChangesCompletedEventData eventData, int result)
73 | {
74 | Console.WriteLine("after save");
75 | return base.SavedChanges(eventData, result);
76 | }
77 | }
78 | }
--------------------------------------------------------------------------------
/UseSaveChangesInterceptor/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace UseSaveChangesInterceptor
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | var ctx = new MyDBContext();
10 |
11 | var c = new Customers
12 | {
13 | Name = "code6421_newitem"
14 | };
15 | ctx.Customers.Add(c);
16 | ctx.SaveChanges();
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/UseSaveChangesInterceptor/UseSaveChangesInterceptor.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/UseSaveChangesInterceptor/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "Myefc_DB2Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": true,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "UseSaveChangesInterceptor",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Customers]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/UseSavepoint/Customers.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace UseSavepoint
8 | {
9 | public partial class Customers
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public string Address { get; set; }
14 | public int CreditLevel { get; set; }
15 | }
16 | }
--------------------------------------------------------------------------------
/UseSavepoint/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace UseSavepoint
9 | {
10 | public partial class MyDBContext : DbContext
11 | {
12 | public MyDBContext()
13 | {
14 | }
15 |
16 | public MyDBContext(DbContextOptions options)
17 | : base(options)
18 | {
19 | }
20 |
21 | public virtual DbSet Customers { get; set; }
22 |
23 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
24 | {
25 | optionsBuilder.UseSqlServer(
26 | @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB2;Integrated Security=True;Connect Timeout=30");
27 | base.OnConfiguring(optionsBuilder);
28 | }
29 |
30 | protected override void OnModelCreating(ModelBuilder modelBuilder)
31 | {
32 | modelBuilder.Entity(entity =>
33 | {
34 | entity.Property(e => e.Address)
35 | .HasMaxLength(250)
36 | .HasColumnName("ADDRESS");
37 |
38 | entity.Property(e => e.CreditLevel).HasColumnName("CREDIT_LEVEL");
39 |
40 | entity.Property(e => e.Name)
41 | .HasMaxLength(120)
42 | .HasColumnName("NAME");
43 | });
44 |
45 | OnModelCreatingPartial(modelBuilder);
46 | }
47 |
48 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
49 | }
50 | }
--------------------------------------------------------------------------------
/UseSavepoint/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace UseSavepoint
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var ctx = new MyDBContext();
11 | using(var trans = ctx.Database.BeginTransaction())
12 | {
13 | var r = ctx.Customers.FirstOrDefault();
14 | r.Name = "testmodify";
15 | ctx.SaveChanges();
16 |
17 | trans.CreateSavepoint("ver1");
18 | //create savepoint ver1
19 | r.Name = "testmodify3";
20 | ctx.SaveChanges(); //commit testmodify3
21 | //rollback to ver1, now the name is testmodify
22 | trans.RollbackToSavepoint("ver1");
23 | trans.Commit();
24 | }
25 | Console.ReadLine();
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/UseSavepoint/UseSavepoint.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/UseSavepoint/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "Myefc_DB2Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": false,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "UseSavepoint",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Customers]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/UseSavingChangesEvent/Customers.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | #nullable disable
6 |
7 | namespace UseSavingChangesEvent
8 | {
9 | public partial class Customers
10 | {
11 | public int Id { get; set; }
12 | public string Name { get; set; }
13 | public string Address { get; set; }
14 | public int CreditLevel { get; set; }
15 | }
16 | }
--------------------------------------------------------------------------------
/UseSavingChangesEvent/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace UseSavingChangesEvent
9 | {
10 | public partial class MyDBContext : DbContext
11 | {
12 | public MyDBContext()
13 | {
14 | }
15 |
16 | public MyDBContext(DbContextOptions options)
17 | : base(options)
18 | {
19 | }
20 |
21 | public virtual DbSet Customers { get; set; }
22 |
23 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
24 | {
25 | if (!optionsBuilder.IsConfigured)
26 | {
27 | #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.
28 | optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyEFC_DB2;Integrated Security=True");
29 | }
30 | }
31 |
32 | protected override void OnModelCreating(ModelBuilder modelBuilder)
33 | {
34 | modelBuilder.Entity(entity =>
35 | {
36 | entity.Property(e => e.Address)
37 | .HasMaxLength(250)
38 | .HasColumnName("ADDRESS");
39 |
40 | entity.Property(e => e.CreditLevel).HasColumnName("CREDIT_LEVEL");
41 |
42 | entity.Property(e => e.Name)
43 | .HasMaxLength(120)
44 | .HasColumnName("NAME");
45 | });
46 |
47 | OnModelCreatingPartial(modelBuilder);
48 | }
49 |
50 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
51 | }
52 | }
--------------------------------------------------------------------------------
/UseSavingChangesEvent/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace UseSavingChangesEvent
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | var ctx = new MyDBContext();
10 | ctx.SavingChanges += Ctx_SavingChanges;
11 | ctx.SavedChanges += Ctx_SavedChanges;
12 |
13 | var c = new Customers
14 | {
15 | Name = "code6421_newitem",
16 | Address = "Tainan"
17 | };
18 | ctx.Customers.Add(c);
19 | ctx.SaveChanges();
20 | }
21 |
22 | private static void Ctx_SavedChanges(object sender, Microsoft.EntityFrameworkCore.SavedChangesEventArgs e)
23 | {
24 | Console.WriteLine("after save");
25 | }
26 |
27 | private static void Ctx_SavingChanges(object sender, Microsoft.EntityFrameworkCore.SavingChangesEventArgs e)
28 | {
29 | Console.WriteLine("before save");
30 | foreach (var item in ((MyDBContext)sender).ChangeTracker.Entries())
31 | {
32 | if (item.State == Microsoft.EntityFrameworkCore.EntityState.Added && item.Entity is Customers)
33 | {
34 | var c = (Customers)item.Entity;
35 | if (string.IsNullOrEmpty(c.Address))
36 | throw new ArgumentException("Address must have value");
37 | }
38 | }
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/UseSavingChangesEvent/UseSavingChangesEvent.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/UseSavingChangesEvent/efpt.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "ContextClassName": "Myefc_DB2Context",
3 | "ContextNamespace": null,
4 | "DefaultDacpacSchema": null,
5 | "FilterSchemas": false,
6 | "IncludeConnectionString": true,
7 | "ModelNamespace": null,
8 | "OutputContextPath": null,
9 | "OutputPath": null,
10 | "ProjectRootNamespace": "UseSavingChangesEvent",
11 | "Schemas": null,
12 | "SelectedHandlebarsLanguage": 0,
13 | "SelectedToBeGenerated": 0,
14 | "Tables": [
15 | {
16 | "HasPrimaryKey": true,
17 | "Name": "[dbo].[Customers]",
18 | "ObjectType": 0
19 | }
20 | ],
21 | "UseDatabaseNames": false,
22 | "UseDbContextSplitting": false,
23 | "UseFluentApiOnly": true,
24 | "UseHandleBars": false,
25 | "UseInflector": false,
26 | "UseLegacyPluralizer": false,
27 | "UseNodaTime": false,
28 | "UseSpatial": false
29 | }
--------------------------------------------------------------------------------
/UseSplitQuery/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Collections;
4 | using System.Collections.Generic;
5 | using Microsoft.EntityFrameworkCore;
6 | using Microsoft.EntityFrameworkCore.Metadata;
7 |
8 | #nullable disable
9 |
10 | namespace UseSplitQuery
11 | {
12 | public class Orders
13 | {
14 | public int Id { get; set; }
15 | public DateTime OrderDate { get; set; }
16 | public string CustomerName { get; set; }
17 |
18 | public virtual ICollection Details { get; set; }
19 |
20 | public Orders()
21 | {
22 | Details = new List();
23 | }
24 |
25 | }
26 |
27 | public class Order_Details
28 | {
29 | public int Id { get; set; }
30 | public Orders Orders { get; set; }
31 | public string Product { get; set; }
32 | public int Qty { get; set; }
33 | public decimal Price { get; set; }
34 | }
35 |
36 | public partial class MyDBContext : DbContext
37 | {
38 | public MyDBContext()
39 | {
40 | }
41 |
42 | public MyDBContext(DbContextOptions options)
43 | : base(options)
44 | {
45 | }
46 |
47 | public virtual DbSet Orders { get; set; }
48 |
49 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
50 | {
51 | optionsBuilder.UseSqlServer(
52 | @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB5;Integrated Security=True;Connect Timeout=30");
53 | optionsBuilder.LogTo(Console.WriteLine);
54 | base.OnConfiguring(optionsBuilder);
55 | }
56 |
57 | protected override void OnModelCreating(ModelBuilder modelBuilder)
58 | {
59 | OnModelCreatingPartial(modelBuilder);
60 | }
61 |
62 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
63 | }
64 | }
--------------------------------------------------------------------------------
/UseSplitQuery/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Microsoft.EntityFrameworkCore;
5 |
6 | namespace UseSplitQuery
7 | {
8 | class Program
9 | {
10 | static void AddData()
11 | {
12 | var ctx = new MyDBContext();
13 | var o = new Orders
14 | {
15 | CustomerName = "code6421",
16 | Details = new List
17 | {
18 | new Order_Details { Product = "C#", Price = 1200, Qty = 1},
19 | new Order_Details { Product = "VBNET", Price = 1700, Qty = 1},
20 | }
21 | };
22 | ctx.Orders.Add(o);
23 | ctx.SaveChanges();
24 | }
25 |
26 | static void QueryDataSingle()
27 | {
28 | var ctx = new MyDBContext();
29 | var r = ctx.Orders.Include(b => b.Details).ToList();
30 | }
31 |
32 | static void QueryDataSplit()
33 | {
34 | var ctx = new MyDBContext();
35 | var r = ctx.Orders.AsSplitQuery().Include(b => b.Details).ToList();
36 | }
37 | static void Main(string[] args)
38 | {
39 | QueryDataSplit();
40 | Console.ReadLine();
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/UseSplitQuery/UseSplitQuery.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/UseTPH/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace UseTPH
9 | {
10 |
11 | public class Employees
12 | {
13 | public int Id { get; set; }
14 | public string Name { get; set; }
15 | }
16 |
17 | public class Managers:Employees
18 | {
19 | public string Role { get; set; }
20 | }
21 |
22 | public partial class MyDBContext : DbContext
23 | {
24 | public MyDBContext()
25 | {
26 | }
27 |
28 | public MyDBContext(DbContextOptions options)
29 | : base(options)
30 | {
31 | }
32 |
33 | public virtual DbSet Employees { get; set; }
34 | public virtual DbSet Managers { get; set; }
35 |
36 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
37 | {
38 | optionsBuilder.UseSqlServer(
39 | @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB6;Integrated Security=True;Connect Timeout=30");
40 | base.OnConfiguring(optionsBuilder);
41 | }
42 |
43 | protected override void OnModelCreating(ModelBuilder modelBuilder)
44 | {
45 | modelBuilder.Entity().HasDiscriminator(
46 | "emptype").HasValue(
47 | "employees").HasValue("managers");
48 |
49 | OnModelCreatingPartial(modelBuilder);
50 | }
51 |
52 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
53 | }
54 | }
--------------------------------------------------------------------------------
/UseTPH/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace UseTPH
4 | {
5 | class Program
6 | {
7 | static void Main(string[] args)
8 | {
9 | var ctx = new MyDBContext();
10 | ctx.Database.EnsureCreated();
11 |
12 | var emp = new Employees
13 | {
14 | Name = "code6421"
15 | };
16 |
17 | var manager = new Managers
18 | {
19 | Name = "jeff",
20 | Role = "RD Leader"
21 | };
22 |
23 | ctx.Employees.Add(emp);
24 | ctx.Managers.Add(manager);
25 | ctx.SaveChanges();
26 |
27 | Console.WriteLine("Hello World!");
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/UseTPH/UseTPH.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/UseTPT/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata;
5 |
6 | #nullable disable
7 |
8 | namespace UseTPT
9 | {
10 |
11 | public class Employees
12 | {
13 | public int Id { get; set; }
14 | public string Name { get; set; }
15 | }
16 |
17 | public class Managers : Employees
18 | {
19 | public string Role { get; set; }
20 | }
21 |
22 | public partial class MyDBContext : DbContext
23 | {
24 | public MyDBContext()
25 | {
26 | }
27 |
28 | public MyDBContext(DbContextOptions options)
29 | : base(options)
30 | {
31 | }
32 |
33 | public virtual DbSet Employees { get; set; }
34 | public virtual DbSet Managers { get; set; }
35 |
36 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
37 | {
38 | optionsBuilder.UseSqlServer(
39 | @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MYEFC_DB6;Integrated Security=True;Connect Timeout=30");
40 | optionsBuilder.LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information);
41 | base.OnConfiguring(optionsBuilder);
42 | }
43 |
44 | protected override void OnModelCreating(ModelBuilder modelBuilder)
45 | {
46 | modelBuilder.Entity().ToTable("Employees");
47 | modelBuilder.Entity().ToTable("Managers");
48 |
49 | OnModelCreatingPartial(modelBuilder);
50 | }
51 |
52 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
53 | }
54 | }
--------------------------------------------------------------------------------
/UseTPT/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace UseTPT
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var ctx = new MyDBContext();
11 | //ctx.Database.EnsureCreated();
12 |
13 | //var emp = new Employees
14 | //{
15 | // Name = "code6421"
16 | //};
17 |
18 | //var manager = new Managers
19 | //{
20 | // Name = "jeff",
21 | // Role = "RD Leader"
22 | //};
23 |
24 | //ctx.Employees.Add(emp);
25 | //ctx.Managers.Add(manager);
26 | //ctx.SaveChanges();
27 | var r = ctx.Managers.FirstOrDefault(a => a.Name.Contains("jeff"));
28 | Console.WriteLine("Hello World!");
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/UseTPT/UseTPT.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/UseTVF/MyDBContext.cs:
--------------------------------------------------------------------------------
1 | // This file has been auto generated by EF Core Power Tools.
2 | using System;
3 | using System.Linq;
4 | using Microsoft.EntityFrameworkCore;
5 | using Microsoft.EntityFrameworkCore.Metadata;
6 |
7 | #nullable disable
8 |
9 | namespace UseTVF
10 | {
11 | public class CustomerReport
12 | {
13 | public int Id { get; set; }
14 | public string NAME { get; set; }
15 | }
16 |
17 | public partial class MyDBContext : DbContext
18 | {
19 | public MyDBContext()
20 | {
21 | }
22 |
23 | public virtual DbSet Customers { get; set; }
24 |
25 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
26 | {
27 | if (!optionsBuilder.IsConfigured)
28 | {
29 | #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.
30 | optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyEFC_DB2;Integrated Security=True");
31 | optionsBuilder.LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information);
32 | }
33 | }
34 |
35 | protected override void OnModelCreating(ModelBuilder modelBuilder)
36 | {
37 | modelBuilder.Entity().HasNoKey();
38 | modelBuilder.HasDbFunction(() => GetCustomerByName(default));
39 | OnModelCreatingPartial(modelBuilder);
40 | }
41 |
42 | public IQueryable GetCustomerByName(string name)
43 | => FromExpression(() => GetCustomerByName(name));
44 |
45 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
46 | }
47 | }
--------------------------------------------------------------------------------
/UseTVF/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace UseTVF
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var ctx = new MyDBContext();
11 | var r = ctx.GetCustomerByName("%17%");
12 | foreach(var item in r.OrderBy(a => a.Id).Take(15))
13 | {
14 | Console.WriteLine($"{item.NAME}");
15 | }
16 | Console.Read();
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/UseTVF/UseTVF.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/UseTVF/tvf_backup.txt:
--------------------------------------------------------------------------------
1 | CREATE FUNCTION [dbo].[GetCustomerByName]
2 | (
3 | @name nvarchar(120)
4 | )
5 | RETURNS @report TABLE
6 | (
7 | Id int,
8 | [Name] nvarchar(120)
9 | )
10 | AS
11 | BEGIN
12 | INSERT @report
13 | SELECT Id, [Name] FROM Customers
14 | WHERE [Name] Like @name
15 | RETURN
16 | END
--------------------------------------------------------------------------------