Skip to content

Commit

Permalink
[automated] Merge branch 'release/5.0' => 'main' (#24101)
Browse files Browse the repository at this point in the history
* Merged PR 12008: [internal/release/5.0] Feed update
* Update branding to 5.0.4
- update to 5.0.3 SDK
    -  also update downlevel runtimes
* Update dependencies from https://github.com/dotnet/arcade build 20210209.6 (#24103)
* InMemory: Allow UnaryExpression.Operand to be converted to nullable for LeftJoin (#23984)
* Correctly set pool size when using AddDbContextFactory (#23983)
  • Loading branch information
dotnet-maestro-bot authored Feb 12, 2021
1 parent 91655d5 commit ede9db2
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 8 deletions.
8 changes: 4 additions & 4 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"tools": {
"dotnet": "5.0.102",
"dotnet": "5.0.103",
"runtimes": {
"dotnet": [
"3.1.11"
"3.1.12"
],
"aspnetcore": [
"3.1.11"
"3.1.12"
]
}
},
"sdk": {
"version": "5.0.102",
"version": "5.0.103",
"allowPrerelease": true,
"rollForward": "latestMajor"
},
Expand Down
3 changes: 3 additions & 0 deletions src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,9 @@ protected override Expression VisitConditional(ConditionalExpression conditional

return Condition(test, ifTrue, ifFalse);
}

protected override Expression VisitUnary(UnaryExpression unaryExpression)
=> unaryExpression.Update(Visit(unaryExpression.Operand));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ public static IServiceCollection AddPooledDbContextFactory<TContext>(
{
Check.NotNull(optionsAction, nameof(optionsAction));

return AddPooledDbContextFactory<TContext>(serviceCollection, (_, ob) => optionsAction(ob));
return AddPooledDbContextFactory<TContext>(serviceCollection, (_, ob) => optionsAction(ob), poolSize);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,75 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

#endregion

#region Issue23926

[ConditionalFact]
public virtual void Left_join_with_entity_with_enum_discriminator()
{
using (CreateScratch<MyContext23926>(Seed23926, "23926"))
{
using var context = new MyContext23926();

var query = context.History.Select(e => e.User.Name).ToList();

Assert.Equal(query, new string[] { "UserA", "DerivedUserB", null });
}
}

private static void Seed23926(MyContext23926 context)
{
context.Add(new History23926 { User = new User23926 { Name = "UserA" } });
context.Add(new History23926 { User = new DerivedUser23926 { Name = "DerivedUserB" } });
context.Add(new History23926 { User = null });

context.SaveChanges();
}

private class History23926
{
public int Id { get; set; }
public int? UserId { get; set; }
public User23926 User { get; set; }
}

private class User23926
{
public int Id { get; set; }
public UserTypes23926 Type { get; set; }
public string Name { get; set; }
}
private enum UserTypes23926
{
User,
DerivedUser
}

private class DerivedUser23926 : User23926
{
public string Value { get; set; }
}

private class MyContext23926 : DbContext
{
public DbSet<History23926> History { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseInternalServiceProvider(InMemoryFixture.DefaultServiceProvider)
.UseInMemoryDatabase("23926");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User23926>().HasDiscriminator(e => e.Type)
.HasValue<User23926>(UserTypes23926.User)
.HasValue<DerivedUser23926>(UserTypes23926.DerivedUser);
}
}

#endregion

#region SharedHelper

private static InMemoryTestStore CreateScratch<TContext>(Action<TContext> seed, string databaseName)
Expand Down
134 changes: 131 additions & 3 deletions test/EFCore.SqlServer.FunctionalTests/DbContextPoolingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.TestUtilities;
Expand All @@ -26,7 +27,41 @@ namespace Microsoft.EntityFrameworkCore
{
public class DbContextPoolingTest : IClassFixture<NorthwindQuerySqlServerFixture<NoopModelCustomizer>>
{
private static IServiceProvider BuildServiceProvider<TContextService, TContext>(int poolSize = 32)
private static IServiceProvider BuildServiceProvider<TContextService, TContext>()
where TContextService : class
where TContext : DbContext, TContextService
=> new ServiceCollection()
.AddDbContextPool<TContextService, TContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).AddDbContextPool<ISecondContext, SecondContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).BuildServiceProvider();

private static IServiceProvider BuildServiceProvider<TContext>()
where TContext : DbContext
=> new ServiceCollection()
.AddDbContextPool<TContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).AddDbContextPool<SecondContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).BuildServiceProvider();

private static IServiceProvider BuildServiceProviderWithFactory<TContext>()
where TContext : DbContext
=> new ServiceCollection()
.AddPooledDbContextFactory<TContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).AddDbContextPool<SecondContext>(
ob =>
ob.UseSqlServer(SqlServerNorthwindTestStoreFactory.NorthwindConnectionString)
.EnableServiceProviderCaching(false)).BuildServiceProvider();

private static IServiceProvider BuildServiceProvider<TContextService, TContext>(int poolSize)
where TContextService : class
where TContext : DbContext, TContextService
=> new ServiceCollection()
Expand All @@ -41,7 +76,7 @@ private static IServiceProvider BuildServiceProvider<TContextService, TContext>(
.EnableServiceProviderCaching(false),
poolSize).BuildServiceProvider();

private static IServiceProvider BuildServiceProvider<TContext>(int poolSize = 32)
private static IServiceProvider BuildServiceProvider<TContext>(int poolSize)
where TContext : DbContext
=> new ServiceCollection()
.AddDbContextPool<TContext>(
Expand All @@ -56,7 +91,7 @@ private static IServiceProvider BuildServiceProvider<TContext>(int poolSize = 32
poolSize)
.BuildServiceProvider();

private static IServiceProvider BuildServiceProviderWithFactory<TContext>(int poolSize = 32)
private static IServiceProvider BuildServiceProviderWithFactory<TContext>(int poolSize)
where TContext : DbContext
=> new ServiceCollection()
.AddPooledDbContextFactory<TContext>(
Expand Down Expand Up @@ -151,13 +186,105 @@ public void Invalid_pool_size()
Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProvider<PooledContext>(poolSize: -1));

Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProvider<IPooledContext, PooledContext>(poolSize: 0));

Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProvider<IPooledContext, PooledContext>(poolSize: -1));

Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProviderWithFactory<PooledContext>(poolSize: 0));

Assert.Throws<ArgumentOutOfRangeException>(
() => BuildServiceProviderWithFactory<PooledContext>(poolSize: -1));
}

[ConditionalFact]
public void Validate_pool_size()
{
var serviceProvider = BuildServiceProvider<PooledContext>(poolSize: 64);

using var scope = serviceProvider.CreateScope();

Assert.Equal(
64,
scope.ServiceProvider
.GetRequiredService<PooledContext>()
.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_with_service_interface()
{
var serviceProvider = BuildServiceProvider<IPooledContext, PooledContext>(poolSize: 64);

using var scope = serviceProvider.CreateScope();

Assert.Equal(
64,
((DbContext)scope.ServiceProvider
.GetRequiredService<IPooledContext>())
.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_with_factory()
{
var serviceProvider = BuildServiceProviderWithFactory<PooledContext>(poolSize: 64);

using var context = serviceProvider.GetRequiredService<IDbContextFactory<PooledContext>>().CreateDbContext();

Assert.Equal(
64,
context.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_default()
{
var serviceProvider = BuildServiceProvider<PooledContext>();

using var scope = serviceProvider.CreateScope();

Assert.Equal(
128,
scope.ServiceProvider
.GetRequiredService<PooledContext>()
.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_with_service_interface_default()
{
var serviceProvider = BuildServiceProvider<IPooledContext, PooledContext>();

using var scope = serviceProvider.CreateScope();

Assert.Equal(
128,
((DbContext)scope.ServiceProvider
.GetRequiredService<IPooledContext>())
.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalFact]
public void Validate_pool_size_with_factory_default()
{
var serviceProvider = BuildServiceProviderWithFactory<PooledContext>();

using var context = serviceProvider.GetRequiredService<IDbContextFactory<PooledContext>>().CreateDbContext();

Assert.Equal(
128,
context.GetService<IDbContextOptions>()
.FindExtension<CoreOptionsExtension>().MaxPoolSize);
}

[ConditionalTheory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -668,6 +795,7 @@ private object GetContextEventField(DbContext context, string eventName)
=> typeof(DbContext)
.GetField(eventName, BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(context);

private bool _changeTracker_OnTracked;

private void ChangeTracker_OnTracked(object sender, EntityTrackedEventArgs e)
Expand Down

0 comments on commit ede9db2

Please sign in to comment.