Skip to content

Commit

Permalink
Add a static flag in EF that will be set when code is being executed …
Browse files Browse the repository at this point in the history
…for design-time discovery

Fixes #27306.
  • Loading branch information
ajcvickers committed Jul 26, 2022
1 parent 024fd3b commit f5c48ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/EFCore.Design/Design/OperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ public abstract class OperationBase : MarshalByRefObject
/// <param name="resultHandler">The <see cref="IOperationResultHandler" />.</param>
protected OperationBase(IOperationResultHandler resultHandler)
{
EF.IsDesignTime = true;
_resultHandler = resultHandler;
}

Expand Down
16 changes: 16 additions & 0 deletions src/EFCore/EF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ public static partial class EF
internal static readonly MethodInfo PropertyMethod
= typeof(EF).GetTypeInfo().GetDeclaredMethod(nameof(Property))!;

/// <summary>
/// This flag is set to <see langword="true" /> when code is being run from a design-time tool, such
/// as "dotnet ef" or one of the Package Manager Console PowerShell commands "Add-Migration", "Update-Database", etc.
/// </summary>
/// <remarks>
/// <para>
/// This flag can be inspected to change application behavior. For example, if the application is being executed by an EF
/// design-time tool, then it may choose to skip executing migrations commands as part of startup.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-commandline">EF Core command-line reference </see> for more information
/// and examples.
/// </para>
/// </remarks>
public static bool IsDesignTime { get; set; }

/// <summary>
/// References a given property or navigation on an entity instance. This is useful for shadow state properties, for
/// which no CLR property exists. Currently this method can only be used in LINQ queries and can not be used to
Expand Down
15 changes: 15 additions & 0 deletions test/EFCore.Design.Tests/Design/OperationExecutorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ public void Ctor_validates_arguments()

public class OperationBaseTests
{
[ConditionalFact]
public void Operations_have_design_time_flag_set()
{
var handler = new OperationResultHandler();
var result = "Twilight Sparkle";

new MockOperation<string>(handler, () =>
{
Assert.True(EF.IsDesignTime);
return result;
});

Assert.Equal(result, handler.Result);
}

[ConditionalFact]
public void Execute_catches_exceptions()
{
Expand Down

0 comments on commit f5c48ba

Please sign in to comment.