Skip to content

Commit

Permalink
Preserve the null table mapping in migration snapshot when an entity …
Browse files Browse the repository at this point in the history
…type is used as a TVF return type. (#25175)

Fixes #25133
  • Loading branch information
AndriySvyryd committed Jul 6, 2021
1 parent 982edfa commit 595974e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
Expand Up @@ -841,13 +841,14 @@ protected virtual void GenerateKeyAnnotations([NotNull] IKey key, [NotNull] Inde
|| entityType.BaseType == null)
{
var tableName = (string)tableNameAnnotation?.Value ?? entityType.GetTableName();
if (tableName != null)
if (tableName != null
|| tableNameAnnotation != null)
{
stringBuilder
.AppendLine()
.Append(builderName)
.Append(".ToTable(")
.Append(Code.Literal(tableName));
.Append(Code.UnknownLiteral(tableName));
if (tableNameAnnotation != null)
{
annotations.Remove(tableNameAnnotation.Name);
Expand Down
Expand Up @@ -216,6 +216,7 @@ private static IEnumerable<IAnnotatable> GetAnnotatables(IModel model)
private IEnumerable<string> GetAnnotationNamespaces(IEnumerable<IAnnotatable> items)
=> items.SelectMany(
i => Dependencies.AnnotationCodeGenerator.FilterIgnoredAnnotations(i.GetAnnotations())
.Where(a => a.Value != null)
.Select(a => new { Annotatable = i, Annotation = a })
.SelectMany(a => GetProviderType(a.Annotatable, a.Annotation.Value.GetType()).GetNamespaces()));

Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Expand Up @@ -69,7 +69,7 @@ public AnnotationCodeGenerator([NotNull] AnnotationCodeGeneratorDependencies dep
public virtual IEnumerable<IAnnotation> FilterIgnoredAnnotations(IEnumerable<IAnnotation> annotations)
=> annotations.Where(
a => !(
a.Value is null
(a.Value is null && a.Name != RelationalAnnotationNames.TableName)
|| CoreAnnotationNames.AllNames.Contains(a.Name)
|| _ignoredRelationalAnnotations.Contains(a.Name)));

Expand Down
Expand Up @@ -544,7 +544,7 @@ public static void SetFunctionName([NotNull] this IMutableEntityType entityType,
[CanBeNull] string name,
bool fromDataAnnotation = false)
=> (string)entityType.SetAnnotation(
RelationalAnnotationNames.ViewName,
RelationalAnnotationNames.FunctionName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation)?.Value;

Expand Down
Expand Up @@ -526,9 +526,7 @@ private static IQueryable<TestKeylessType> GetCountByYear(int id)
=> throw new NotImplementedException();

[ConditionalFact]
public void TVF_types_are_stored_in_the_model_snapshot()
{
Test(
public void TVF_types_are_stored_in_the_model_snapshot() => Test(
builder =>
{
builder.HasDbFunction(
Expand All @@ -545,9 +543,15 @@ public void TVF_types_are_stored_in_the_model_snapshot()
{
b.Property<string>(""Something"")
.HasColumnType(""nvarchar(max)"");
b.ToTable(null);
});"),
o => Assert.Null(o.GetEntityTypes().Single().GetFunctionName()));
}
o =>
{
var entityType = o.GetEntityTypes().Single();
Assert.Null(entityType.GetFunctionName());
Assert.Null(entityType.GetTableName());
});

[ConditionalFact]
public void Entity_types_mapped_to_functions_are_stored_in_the_model_snapshot()
Expand Down

0 comments on commit 595974e

Please sign in to comment.