Skip to content

Commit

Permalink
Register design-time LoggerFactory as Scoped
Browse files Browse the repository at this point in the history
Fixes #26384
  • Loading branch information
AndriySvyryd committed Oct 25, 2021
1 parent 7261b64 commit 0e2d130
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices(
.TryAddScoped<IMigrationsScaffolder, MigrationsScaffolder>()
.TryAddScoped<ISnapshotModelProcessor, SnapshotModelProcessor>());

return services
.AddLogging(b => b.SetMinimumLevel(LogLevel.Debug).AddProvider(new OperationLoggerProvider(reporter)));
var loggerFactory = new LoggerFactory(new[] { new OperationLoggerProvider(reporter) }, new LoggerFilterOptions { MinLevel = LogLevel.Debug });
services.AddScoped<ILoggerFactory>(_ => loggerFactory);

return services;
}

/// <summary>
Expand Down
5 changes: 0 additions & 5 deletions src/EFCore/Design/EntityFrameworkDesignServicesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public class EntityFrameworkDesignServicesBuilder : EntityFrameworkServicesBuild
public static readonly IDictionary<Type, ServiceCharacteristics> Services
= new Dictionary<Type, ServiceCharacteristics>
{
{ typeof(IDbContextLogger), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IDiagnosticsLogger<>), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ICSharpRuntimeAnnotationCodeGenerator), new ServiceCharacteristics(ServiceLifetime.Singleton) }
};

Expand Down Expand Up @@ -81,9 +79,6 @@ public EntityFrameworkDesignServicesBuilder(IServiceCollection serviceCollection
/// <returns>This builder, such that further calls can be chained.</returns>
public override EntityFrameworkServicesBuilder TryAddCoreServices()
{
TryAdd<IDbContextLogger, NullDbContextLogger>();
TryAdd(typeof(IDiagnosticsLogger<>), typeof(DiagnosticsLogger<>));
TryAdd<ILoggingOptions, LoggingOptions>();
TryAdd<ICSharpRuntimeAnnotationCodeGenerator, CSharpRuntimeAnnotationCodeGenerator>();

ServiceCollectionMap.GetInfrastructure()
Expand Down
70 changes: 37 additions & 33 deletions test/EFCore.Cosmos.FunctionalTests/Query/FromSqlQueryCosmosTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected NorthwindContext CreateContext()
public async Task FromSqlRaw_queryable_simple(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>()
.FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""ContactName""] LIKE '%z%'");
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""ContactName""] LIKE '%z%'");

var actual = async
? await query.ToArrayAsync()
Expand All @@ -56,8 +56,8 @@ public async Task FromSqlRaw_queryable_simple(bool async)
public async Task FromSqlRaw_queryable_incorrect_discriminator_throws(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>()
.FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Order""");
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Order""");

var exception = async
? await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToArrayAsync())
Expand All @@ -73,7 +73,7 @@ public async Task FromSqlRaw_queryable_incorrect_discriminator_throws(bool async
public async Task FromSqlRaw_queryable_simple_columns_out_of_order(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT c[""id""], c[""Discriminator""], c[""Region""], c[""PostalCode""], c[""Phone""], c[""Fax""], c[""CustomerID""], c[""Country""], c[""ContactTitle""], c[""ContactName""], c[""CompanyName""], c[""City""], c[""Address""] FROM root c WHERE c[""Discriminator""] = ""Customer""");

var actual = async
Expand All @@ -95,7 +95,7 @@ public async Task FromSqlRaw_queryable_simple_columns_out_of_order(bool async)
public async Task FromSqlRaw_queryable_simple_columns_out_of_order_and_extra_columns(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT c[""id""], c[""Discriminator""], c[""Region""], c[""PostalCode""], c[""PostalCode""] AS Foo, c[""Phone""], c[""Fax""], c[""CustomerID""], c[""Country""], c[""ContactTitle""], c[""ContactName""], c[""CompanyName""], c[""City""], c[""Address""] FROM root c WHERE c[""Discriminator""] = ""Customer""");

var actual = async
Expand All @@ -117,7 +117,7 @@ public async Task FromSqlRaw_queryable_simple_columns_out_of_order_and_extra_col
public async Task FromSqlRaw_queryable_composed(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""").Where(c => c.ContactName.Contains("z"));

var sql = query.ToQueryString();
Expand All @@ -142,7 +142,7 @@ public async Task FromSqlRaw_queryable_composed(bool async)
public virtual async Task FromSqlRaw_queryable_composed_after_removing_whitespaces(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
_eol + " " + _eol + _eol + _eol + "SELECT" + _eol + @"* FROM root c WHERE c[""Discriminator""] = ""Customer""")
.Where(c => c.ContactName.Contains("z"));

Expand Down Expand Up @@ -172,8 +172,8 @@ public async Task FromSqlRaw_queryable_composed_compiled(bool async)
if (async)
{
var query = EF.CompileAsyncQuery(
(NorthwindContext context) => context.Set<Customer>()
.FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
(NorthwindContext context) => CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
.Where(c => c.ContactName.Contains("z")));

using (var context = CreateContext())
Expand All @@ -186,8 +186,8 @@ public async Task FromSqlRaw_queryable_composed_compiled(bool async)
else
{
var query = EF.CompileQuery(
(NorthwindContext context) => context.Set<Customer>()
.FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
(NorthwindContext context) => CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
.Where(c => c.ContactName.Contains("z")));

using (var context = CreateContext())
Expand All @@ -213,8 +213,8 @@ public virtual async Task FromSqlRaw_queryable_composed_compiled_with_parameter(
if (async)
{
var query = EF.CompileAsyncQuery(
(NorthwindContext context) => context.Set<Customer>()
.FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""CustomerID""] = {0}", "CONSH")
(NorthwindContext context) => CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""CustomerID""] = {0}", "CONSH")
.Where(c => c.ContactName.Contains("z")));

using (var context = CreateContext())
Expand All @@ -227,8 +227,8 @@ public virtual async Task FromSqlRaw_queryable_composed_compiled_with_parameter(
else
{
var query = EF.CompileQuery(
(NorthwindContext context) => context.Set<Customer>()
.FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""CustomerID""] = {0}", "CONSH")
(NorthwindContext context) => CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""CustomerID""] = {0}", "CONSH")
.Where(c => c.ContactName.Contains("z")));

using (var context = CreateContext())
Expand All @@ -252,7 +252,7 @@ public virtual async Task FromSqlRaw_queryable_composed_compiled_with_parameter(
public virtual async Task FromSqlRaw_queryable_multiple_line_query(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT *
FROM root c
WHERE c[""Discriminator""] = ""Customer"" AND c[""City""] = 'London'");
Expand All @@ -278,7 +278,7 @@ FROM root c
public virtual async Task FromSqlRaw_queryable_composed_multiple_line_query(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT *
FROM root c
WHERE c[""Discriminator""] = ""Customer""")
Expand Down Expand Up @@ -309,7 +309,7 @@ public async Task FromSqlRaw_queryable_with_parameters(bool async)
var contactTitle = "Sales Representative";

using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""City""] = {0} AND c[""ContactTitle""] = {1}", city,
contactTitle);

Expand All @@ -336,7 +336,7 @@ SELECT c
public async Task FromSqlRaw_queryable_with_parameters_inline(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""City""] = {0} AND c[""ContactTitle""] = {1}", "London",
"Sales Representative");

Expand Down Expand Up @@ -365,7 +365,7 @@ public async Task FromSqlRaw_queryable_with_null_parameter(bool async)
uint? reportsTo = null;

using var context = CreateContext();
var query = context.Set<Employee>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Employee>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Employee"" AND c[""ReportsTo""] = {0} OR (IS_NULL(c[""ReportsTo""]) AND IS_NULL({0}))", reportsTo);

var actual = async
Expand All @@ -391,7 +391,7 @@ public async Task FromSqlRaw_queryable_with_parameters_and_closure(bool async)
var contactTitle = "Sales Representative";

using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""City""] = {0}", city)
.Where(c => c.ContactTitle == contactTitle);
var queryString = query.ToQueryString();
Expand Down Expand Up @@ -420,8 +420,8 @@ SELECT c
public virtual async Task FromSqlRaw_queryable_simple_cache_key_includes_query_string(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>()
.FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""City""] = 'London'");
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""City""] = 'London'");

var actual = async
? await query.ToArrayAsync()
Expand All @@ -430,8 +430,8 @@ public virtual async Task FromSqlRaw_queryable_simple_cache_key_includes_query_s
Assert.Equal(6, actual.Length);
Assert.True(actual.All(c => c.City == "London"));

query = context.Set<Customer>()
.FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""City""] = 'Seattle'");
query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""City""] = 'Seattle'");

actual = async
? await query.ToArrayAsync()
Expand Down Expand Up @@ -461,7 +461,7 @@ public virtual async Task FromSqlRaw_queryable_with_parameters_cache_key_include
var sql = @"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer"" AND c[""City""] = {0} AND c[""ContactTitle""] = {1}";

using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(sql, city, contactTitle);
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(), sql, city, contactTitle);

var actual = async
? await query.ToArrayAsync()
Expand All @@ -474,7 +474,7 @@ public virtual async Task FromSqlRaw_queryable_with_parameters_cache_key_include
city = "Madrid";
contactTitle = "Accounting Manager";

query = context.Set<Customer>().FromSqlRaw(sql, city, contactTitle);
query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(), sql, city, contactTitle);

actual = async
? await query.ToArrayAsync()
Expand Down Expand Up @@ -507,7 +507,8 @@ SELECT c
public virtual async Task FromSqlRaw_queryable_simple_as_no_tracking_not_composed(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
.AsNoTracking();

var actual = async
Expand All @@ -529,7 +530,7 @@ public virtual async Task FromSqlRaw_queryable_simple_as_no_tracking_not_compose
public virtual async Task FromSqlRaw_queryable_simple_projection_composed(bool async)
{
using var context = CreateContext();
var query = context.Set<Product>().FromSqlRaw(
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Product>(),
@"SELECT *
FROM root c
WHERE c[""Discriminator""] = ""Product"" AND NOT c[""Discontinued""] AND ((c[""UnitsInStock""] + c[""UnitsOnOrder""]) < c[""ReorderLevel""])")
Expand All @@ -555,7 +556,8 @@ FROM root c
public virtual async Task FromSqlRaw_composed_with_nullable_predicate(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
.Where(c => c.ContactName == c.CompanyName);

var actual = async
Expand All @@ -579,7 +581,8 @@ public virtual async Task FromSqlRaw_does_not_parameterize_interpolated_string(b
using var context = CreateContext();
var propertyName = "OrderID";
var max = 10250;
var query = context.Orders.FromSqlRaw($@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Order"" AND c[""{propertyName}""] < {{0}}", max);
var query = CosmosQueryableExtensions.FromSqlRaw(context.Orders,
$@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Order"" AND c[""{propertyName}""] < {{0}}", max);

var actual = async
? await query.ToListAsync()
Expand All @@ -593,7 +596,8 @@ public virtual async Task FromSqlRaw_does_not_parameterize_interpolated_string(b
public virtual async Task FromSqlRaw_queryable_simple_projection_not_composed(bool async)
{
using var context = CreateContext();
var query = context.Set<Customer>().FromSqlRaw(@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
var query = CosmosQueryableExtensions.FromSqlRaw(context.Set<Customer>(),
@"SELECT * FROM root c WHERE c[""Discriminator""] = ""Customer""")
.Select(
c => new { c.CustomerID, c.City })
.AsNoTracking();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public void Log_dampens_logLevel_when_CommandExecuted()

Assert.Collection(
reporter.Messages,
x => Assert.Equal("verbose: -- Can't stop the SQL", x));
x =>
{
Assert.Equal("-- Can't stop the SQL", x.Message);
Assert.Equal(LogLevel.Debug, x.Level);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Ownership;
using Xunit;

Expand Down Expand Up @@ -95,7 +96,9 @@ public void Warns_for_conflicting_annotations()

new SnapshotModelProcessor(reporter, DummyModelRuntimeInitializer.Instance).Process(model);

Assert.Equal("warn: " + DesignStrings.MultipleAnnotationConflict("DefaultSchema"), reporter.Messages.Single());
var (level, message) = reporter.Messages.Single();
Assert.Equal(LogLevel.Warning, level);
Assert.Equal(DesignStrings.MultipleAnnotationConflict("DefaultSchema"), message);
Assert.Equal(2, model.GetAnnotations().Count());

var actual = (string)model["Relational:DefaultSchema"];
Expand All @@ -116,7 +119,9 @@ public void Warns_for_conflicting_annotations_one_relational()

new SnapshotModelProcessor(reporter, DummyModelRuntimeInitializer.Instance).Process(model);

Assert.Equal("warn: " + DesignStrings.MultipleAnnotationConflict("DefaultSchema"), reporter.Messages.Single());
var (level, message) = reporter.Messages.Single();
Assert.Equal(LogLevel.Warning, level);
Assert.Equal(DesignStrings.MultipleAnnotationConflict("DefaultSchema"), message);
Assert.Equal(2, model.GetAnnotations().Count());

var actual = (string)model["Relational:DefaultSchema"];
Expand Down
Loading

0 comments on commit 0e2d130

Please sign in to comment.