Skip to content

Commit

Permalink
Merge branch 'release/9.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Mar 7, 2025
2 parents 34ee81a + c18fc67 commit 3c9c35e
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 77 deletions.
26 changes: 26 additions & 0 deletions src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,30 @@ private IServiceProvider CreateEmptyServiceProvider()

return new ServiceCollection().BuildServiceProvider();
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static void SetEnvironment(IOperationReporter reporter)
{
var aspnetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var dotnetEnvironment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
var environment = aspnetCoreEnvironment
?? dotnetEnvironment
?? "Development";
if (aspnetCoreEnvironment == null)
{
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", environment);
}

if (dotnetEnvironment == null)
{
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", environment);
}

reporter.WriteVerbose(DesignStrings.UsingEnvironment(environment));
}
}
3 changes: 3 additions & 0 deletions src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.EntityFrameworkCore.Design.Internal;
/// </summary>
public class DatabaseOperations
{
private readonly IOperationReporter _reporter;
private readonly string _projectDir;
private readonly string? _rootNamespace;
private readonly string? _language;
Expand All @@ -34,6 +35,7 @@ public DatabaseOperations(
bool nullable,
string[]? args)
{
_reporter = reporter;
_projectDir = projectDir;
_rootNamespace = rootNamespace;
_language = language;
Expand Down Expand Up @@ -73,6 +75,7 @@ public virtual SavedModelFiles ScaffoldContext(
? Path.GetFullPath(Path.Combine(_projectDir, outputContextDir))
: outputDir;

AppServiceProviderFactory.SetEnvironment(_reporter);
var services = _servicesBuilder.Build(provider);
using var scope = services.CreateScope();

Expand Down
17 changes: 1 addition & 16 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,22 +503,7 @@ private IDictionary<Type, Func<DbContext>> FindContextTypes(string? name = null,
{
_reporter.WriteVerbose(DesignStrings.FindingContexts);

var aspnetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var dotnetEnvironment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
var environment = aspnetCoreEnvironment
?? dotnetEnvironment
?? "Development";
if (aspnetCoreEnvironment == null)
{
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", environment);
}

if (dotnetEnvironment == null)
{
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", environment);
}

_reporter.WriteVerbose(DesignStrings.UsingEnvironment(environment));
AppServiceProviderFactory.SetEnvironment(_reporter);

var contexts = new Dictionary<Type, Func<DbContext>?>();

Expand Down
70 changes: 68 additions & 2 deletions test/EFCore.Design.Tests/Design/Internal/DatabaseOperationsTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Design.Internal;

public class DatabaseOperationsTest
Expand All @@ -10,16 +12,80 @@ public void Can_pass_null_args()
{
// Even though newer versions of the tools will pass an empty array
// older versions of the tools can pass null args.
CreateOperations(null);
}

[ConditionalFact]
public void ScaffoldContext_throws_exceptions_for_invalid_context_name()
{
ValidateContextNameInReverseEngineerGenerator("Invalid!CSharp*Class&Name");
ValidateContextNameInReverseEngineerGenerator("1CSharpClassNameCannotStartWithNumber");
ValidateContextNameInReverseEngineerGenerator("volatile");
}

private void ValidateContextNameInReverseEngineerGenerator(string contextName)
{
var operations = CreateOperations([]);

Assert.Equal(
DesignStrings.ContextClassNotValidCSharpIdentifier(contextName),
Assert.Throws<ArgumentException>(
() => operations.ScaffoldContext(
"Microsoft.EntityFrameworkCore.SqlServer",
"connectionstring",
"",
"",
dbContextClassName: contextName,
null,
null,
"FakeNamespace",
contextNamespace: null,
useDataAnnotations: false,
overwriteFiles: true,
useDatabaseNames: false,
suppressOnConfiguring: true,
noPluralize: false))
.Message);
}

[ConditionalFact]
[SqlServerConfiguredCondition]
public void ScaffoldContext_sets_environment()
{
var operations = CreateOperations([]);
operations.ScaffoldContext(
"Microsoft.EntityFrameworkCore.SqlServer",
TestEnvironment.DefaultConnection,
"",
"",
dbContextClassName: nameof(TestContext),
schemas: ["Empty"],
null,
null,
contextNamespace: null,
useDataAnnotations: false,
overwriteFiles: true,
useDatabaseNames: false,
suppressOnConfiguring: true,
noPluralize: false);

Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"));
}

private static DatabaseOperations CreateOperations(string[] args)
{
var assembly = MockAssembly.Create(typeof(TestContext));
_ = new TestDatabaseOperations(
var operations = new DatabaseOperations(
new TestOperationReporter(),
assembly,
assembly,
"projectDir",
"RootNamespace",
"C#",
nullable: false,
args: null);
args: args);
return operations;
}

public class TestContext : DbContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2453,7 +2453,7 @@ public void InsertDataOperation_required_empty_array()
Assert.Single(o.Columns);
Assert.Equal(1, o.Values.GetLength(0));
Assert.Equal(1, o.Values.GetLength(1));
Assert.Equal([], (string[])o.Values[0, 0]);
Assert.Equal(new string[0], (string[])o.Values[0, 0]);
});

[ConditionalFact]
Expand All @@ -2478,7 +2478,7 @@ public void InsertDataOperation_required_empty_array_composite()
Assert.Equal(1, o.Values.GetLength(0));
Assert.Equal(3, o.Values.GetLength(1));
Assert.Null(o.Values[0, 1]);
Assert.Equal([], (string[])o.Values[0, 2]);
Assert.Equal(new string[0], (string[])o.Values[0, 2]);
});

[ConditionalFact]
Expand Down

This file was deleted.

16 changes: 0 additions & 16 deletions test/EFCore.Design.Tests/TestUtilities/TestDatabaseOperations.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ public void Navigations_on_base_type_should_be_inherited()
var specialCustomerType = model.AddEntityType(typeof(SpecialCustomer));

Assert.Equal(new[] { "Orders" }, customerType.GetNavigations().Select(p => p.Name).ToArray());
Assert.Equal([], specialCustomerType.GetNavigations().Select(p => p.Name).ToArray());
Assert.Equal(new string[0], specialCustomerType.GetNavigations().Select(p => p.Name).ToArray());

specialCustomerType.BaseType = customerType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Can_convert_bytes_to_strings()
var converter = _bytesToStringConverter.ConvertFromProviderExpression.Compile();

Assert.Equal(new byte[] { 83, 112, 196, 177, 110, 204, 136, 97, 108, 32, 84, 97, 112 }, converter("U3DEsW7MiGFsIFRhcA=="));
Assert.Equal([], converter(""));
Assert.Equal(new byte[0], converter(""));
}

[ConditionalFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Can_convert_strings_to_UTF8()
var converter = _stringToUtf8Converter.ConvertToProviderExpression.Compile();

Assert.Equal(new byte[] { 83, 112, 196, 177, 110, 204, 136, 97, 108, 32, 84, 97, 112 }, converter("Spın̈al Tap"));
Assert.Equal([], converter(""));
Assert.Equal(new byte[0], converter(""));
}

[ConditionalFact]
Expand Down

0 comments on commit 3c9c35e

Please sign in to comment.