Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure IMutableAnnotatable methods correctly update the ConfigurationSource #22229

Merged
merged 1 commit into from
Aug 26, 2020
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
26 changes: 25 additions & 1 deletion src/EFCore/Infrastructure/ConventionAnnotatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Microsoft.EntityFrameworkCore.Infrastructure
/// not used in application code.
/// </para>
/// </summary>
public abstract class ConventionAnnotatable : Annotatable, IConventionAnnotatable
public abstract class ConventionAnnotatable : Annotatable, IConventionAnnotatable, IMutableAnnotatable
{
/// <summary>
/// Gets all annotations on the current object.
Expand Down Expand Up @@ -135,6 +135,30 @@ IEnumerable<IConventionAnnotation> IConventionAnnotatable.GetAnnotations()
IConventionAnnotation IConventionAnnotatable.SetAnnotation(string name, object value, bool fromDataAnnotation)
=> SetAnnotation(name, value, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

/// <inheritdoc />
[DebuggerStepThrough]
void IMutableAnnotatable.SetAnnotation(string name, object value)
=> SetAnnotation(name, value, ConfigurationSource.Explicit);

/// <inheritdoc />
object IMutableAnnotatable.this[string name]
{
[DebuggerStepThrough]
get => this[name];
[DebuggerStepThrough]
set
{
if (value == null)
{
RemoveAnnotation(name);
}
else
{
SetAnnotation(name, value, ConfigurationSource.Explicit);
}
}
}

/// <inheritdoc />
[DebuggerStepThrough]
IConventionAnnotation IConventionAnnotatable.AddAnnotation(string name, object value, bool fromDataAnnotation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -123,6 +124,24 @@ public void Test_new_annotations_handled_for_entity_types()
+ _nl
+ @" .HasComment(""My Comment"");"
+ _nl)
},
{
#pragma warning disable CS0612 // Type or member is obsolete
CoreAnnotationNames.DefiningQuery,
#pragma warning restore CS0612 // Type or member is obsolete
(Expression.Lambda(Expression.Constant(null)) , "")
},
{
RelationalAnnotationNames.ViewName,
("MyView", _nl + "modelBuilder." + nameof(RelationalEntityTypeBuilderExtensions.ToView) + @"(""MyView"");" + _nl)
},
{
RelationalAnnotationNames.FunctionName,
(null, "")
},
{
RelationalAnnotationNames.SqlQuery,
(null, "")
}
};

Expand Down Expand Up @@ -246,8 +265,7 @@ private static void MissingAnnotationCheck(
var sqlServerAnnotationCodeGenerator = new SqlServerAnnotationCodeGenerator(
new AnnotationCodeGeneratorDependencies(sqlServerTypeMappingSource));

var codeHelper = new CSharpHelper(
sqlServerTypeMappingSource);
var codeHelper = new CSharpHelper(sqlServerTypeMappingSource);

var generator = new TestCSharpSnapshotGenerator(
new CSharpSnapshotGeneratorDependencies(codeHelper, sqlServerTypeMappingSource, sqlServerAnnotationCodeGenerator));
Expand All @@ -272,9 +290,9 @@ private static void MissingAnnotationCheck(
{
var modelBuilder = RelationalTestHelpers.Instance.CreateConventionBuilder();
var metadataItem = createMetadataItem(modelBuilder);
metadataItem[annotationName] = validAnnotations.ContainsKey(annotationName)
metadataItem.SetAnnotation(annotationName, validAnnotations.ContainsKey(annotationName)
? validAnnotations[annotationName].Value
: null;
: null);

modelBuilder.FinalizeModel();

Expand All @@ -290,11 +308,18 @@ private static void MissingAnnotationCheck(
Assert.False(true, $"Annotation '{annotationName}' was not handled by the code generator: {e.Message}");
}

Assert.Equal(
try
{
Assert.Equal(
validAnnotations.ContainsKey(annotationName)
? validAnnotations[annotationName].Expected
: generationDefault,
sb.ToString());
}
catch (Exception e)
{
throw new Exception(annotationName, e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class UpdatesContext : PoolableDbContext
public DbSet<Product> Products { get; set; }
public DbSet<ProductWithBytes> ProductWithBytes { get; set; }
public DbSet<AFewBytes> AFewBytes { get; set; }
public DbSet<ProductViewTable> ProductView { get; set; }
public DbSet<ProductTableWithView> ProductTable { get; set; }
public DbSet<ProductTableView> ProductTableView { get; set; }

public UpdatesContext(DbContextOptions options)
: base(options)
Expand Down