Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
- name: Restore dependencies
run: dotnet restore
with:
dotnet-version: 8.0.x
- name: Build
run: dotnet build --no-restore
run: dotnet build
# - name: Test
# run: dotnet test --no-build --verbosity normal
6 changes: 3 additions & 3 deletions BackingFields/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace BackingFields;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=BackingFields;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("BackingFields"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions BufferingAndStreaming/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace BufferingAndStreaming;
Expand All @@ -10,8 +11,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=BufferingAndStreaming;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("BufferingAndStreaming"));

optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions BulkDeletes/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace BulkDeletes;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=BulkDeletes;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("BulkDeletes"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions BulkUpdates/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace BulkUpdates;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=BulkUpdate;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("BulkUpdate"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
13 changes: 13 additions & 0 deletions Common/Common.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions Common/DbConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.Data.SqlClient;

namespace Common;

public static class DbConnectionFactory
{
public static string Create(string databaseName)
{
var builder = new SqlConnectionStringBuilder
{
DataSource = "localhost",
InitialCatalog = databaseName,
UserID = "sa",
Password = "yourStrong(!)Password",
TrustServerCertificate = true,
MultipleActiveResultSets = true
};

Console.WriteLine($"Generated connection string: {builder.ConnectionString}");

return builder.ConnectionString;
}
}
6 changes: 3 additions & 3 deletions CompiledModels/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CompiledModels.CompiledModels;
using Common;
using CompiledModels.CompiledModels;
using Microsoft.EntityFrameworkCore;

namespace CompiledModels;
Expand All @@ -10,8 +11,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=CompiledModels;Trusted_Connection=True")
.UseSqlServer(DbConnectionFactory.Create("CompiledModels"))
.UseModel(ApplicationDbContextModel.Instance);

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
Expand Down
6 changes: 3 additions & 3 deletions CompiledQueries/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace CompiledQueries;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=CompiledQueries;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("CompiledQueries"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions DateOnlyTimeOnly/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace DateOnlyTimeOnly;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=DateOnlyTimeOnly;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("DateOnlyTimeOnly"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
4 changes: 4 additions & 0 deletions DateOnlyTimeOnly/DateOnlyTimeOnly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>

</Project>
7 changes: 4 additions & 3 deletions DbContextPooling/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DbContextPooling;
using Common;
using DbContextPooling;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -8,7 +9,7 @@
Console.WriteLine("Pooling WITHOUT Dependency Injection");

var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=DbContextPooling;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("DbContextPooling"));

var factory = new PooledDbContextFactory<ApplicationDbContext>(options.Options);

Expand All @@ -20,7 +21,7 @@

var services = new ServiceCollection();
services.AddDbContextPool<ApplicationDbContext>(
o => o.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=DbContextPooling;Trusted_Connection=True"));
o => o.UseSqlServer(DbConnectionFactory.Create("DbContextPooling")));

var sp = services.BuildServiceProvider();

Expand Down
5 changes: 0 additions & 5 deletions Directory.Build.Props

This file was deleted.

8 changes: 8 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<TreatWarningsAsErrors Condition="'$(Configuration)' != 'Debug'">true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup Condition="'$(MSBuildProjectName)' != 'Common'">
<ProjectReference Include="../Common/Common.csproj"/>
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions EfCoreSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BufferingAndStreaming", "Bu
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompiledModels", "CompiledModels\CompiledModels.csproj", "{01B7FAFB-DA74-4259-B2C3-4DAC9154D8E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "Common\Common.csproj", "{7047FA95-2543-4221-A749-35AE5E92B716}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -152,6 +154,10 @@ Global
{01B7FAFB-DA74-4259-B2C3-4DAC9154D8E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01B7FAFB-DA74-4259-B2C3-4DAC9154D8E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01B7FAFB-DA74-4259-B2C3-4DAC9154D8E8}.Release|Any CPU.Build.0 = Release|Any CPU
{7047FA95-2543-4221-A749-35AE5E92B716}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7047FA95-2543-4221-A749-35AE5E92B716}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7047FA95-2543-4221-A749-35AE5E92B716}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7047FA95-2543-4221-A749-35AE5E92B716}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 4 additions & 4 deletions JsonColumns/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace JsonColumns;

public class ApplicationDbContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
public DbSet<Contact> Contacts { get; set; } = null!;

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=JsonColumns;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("JsonColumns"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
4 changes: 2 additions & 2 deletions JsonColumns/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
public class Contact
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public Address? Address { get; set; }
public string Name { get; init; } = null!;
public required Address Address { get; set; } = null!;
public ICollection<Note>? Notes { get; set; }

public override string ToString()
Expand Down
56 changes: 17 additions & 39 deletions JsonColumns/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,32 @@

var contacts = new List<Contact>
{
new Contact
new()
{
Name = "John Doe",
Address = new Address
{
Line1 = "123 Main St.",
Line2 = "Suite 101",
City = "Seattle",
State = "WA"
},
Notes = new List<Note>
{
new Note { Text = "Note 1"},
new Note { Text = "Note 2"},
new Note { Text = "Note 3"}
}
Address = new Address { Line1 = "123 Main St.", Line2 = "Suite 101", City = "Seattle", State = "WA" },
Notes =
new List<Note>
{
new() { Text = "Note 1" }, new() { Text = "Note 2" }, new() { Text = "Note 3" }
}
},
new Contact
new()
{
Name = "Jane Doe",
Address = new Address
{
Line1 = "456 Main St.",
Line2 = "Suite 202",
City = "Seattle",
State = "WA"
},
Address = new Address { Line1 = "456 Main St.", Line2 = "Suite 202", City = "Seattle", State = "WA" },
Notes = new List<Note>
{
new Note { Text = "Note 4"},
new Note { Text = "Note 5"},
new Note { Text = "Note 6"}
new() { Text = "Note 4" }, new() { Text = "Note 5" }, new() { Text = "Note 6" }
}
},
new Contact
new()
{
Name = "John Smith",
Address = new Address
{
Line1 = "789 Main St.",
Line2 = "Suite 303",
City = "Seattle",
State = "WA"
},
Address = new Address { Line1 = "789 Main St.", Line2 = "Suite 303", City = "Seattle", State = "WA" },
Notes = new List<Note>
{
new Note { Text = "Note 7"},
new Note { Text = "Note 8"},
new Note { Text = "Note 9"}
new() { Text = "Note 7" }, new() { Text = "Note 8" }, new() { Text = "Note 9" }
}
}
};
Expand All @@ -72,6 +49,8 @@
.First(c => c.Name == "John Smith")
.Notes;

ArgumentNullException.ThrowIfNull(notes);

foreach (var note in notes)
Console.WriteLine(note);

Expand All @@ -82,8 +61,7 @@

Console.WriteLine("Deleting Data");
var contactToDelete = db.Contacts.First(c => c.Name == "Jane Doe");
contact.Address = null;
contactToDelete.Address = new Address();
db.SaveChanges();

Console.ReadLine();

Console.ReadLine();
6 changes: 3 additions & 3 deletions KeysetPagination/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace KeysetPagination;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=KeysetPagination;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("KeysetPagination"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions MigrationBundles/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace MigrationBundles;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=MigrationBundles;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("MigrationBundles"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
Loading