diff --git a/EFCore.NamingConventions.Test/NameRewritingConventionTest.cs b/EFCore.NamingConventions.Test/NameRewritingConventionTest.cs index 8473f93..d9f7a9c 100644 --- a/EFCore.NamingConventions.Test/NameRewritingConventionTest.cs +++ b/EFCore.NamingConventions.Test/NameRewritingConventionTest.cs @@ -404,6 +404,14 @@ namespace EFCore.NamingConventions.Test .GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value)); } + [Fact] + public void Not_mapped_to_table() + { + var entityType = BuildEntityType(b => b.Entity().ToSqlQuery("SELECT foobar")); + + Assert.Null(entityType.GetTableName()); + } + private IEntityType BuildEntityType(Action builderAction, CultureInfo culture = null) => BuildModel(builderAction, culture).GetEntityTypes().Single(); diff --git a/EFCore.NamingConventions/Internal/NameRewritingConvention.cs b/EFCore.NamingConventions/Internal/NameRewritingConvention.cs index db36b96..0f5d258 100644 --- a/EFCore.NamingConventions/Internal/NameRewritingConvention.cs +++ b/EFCore.NamingConventions/Internal/NameRewritingConvention.cs @@ -109,6 +109,16 @@ namespace EFCore.NamingConventions.Internal { var entityType = entityTypeBuilder.Metadata; + // If the View/SqlQuery/Function name is being set on the entity type, and its table name is set by convention, then we assume + // we're the one who set the table name back when the entity type was originally added. We now undo this as the entity type + // should only be mapped to the View/SqlQuery/Function. + if (name is RelationalAnnotationNames.ViewName or RelationalAnnotationNames.SqlQuery or RelationalAnnotationNames.FunctionName + && annotation.Value is not null + && entityType.GetTableNameConfigurationSource() == ConfigurationSource.Convention) + { + entityType.SetTableName(null); + } + if (name != RelationalAnnotationNames.TableName || StoreObjectIdentifier.Create(entityType, StoreObjectType.Table) is not StoreObjectIdentifier tableIdentifier) {