Don't create table for View/SqlQuery/Function-mapped entity types (#104)

Fixes #94
This commit is contained in:
Shay Rojansky
2021-11-10 13:08:28 +01:00
committed by GitHub
parent 942d8516a9
commit a447770a14
2 changed files with 18 additions and 0 deletions

View File

@@ -404,6 +404,14 @@ namespace EFCore.NamingConventions.Test
.GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value)); .GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table)!.Value));
} }
[Fact]
public void Not_mapped_to_table()
{
var entityType = BuildEntityType(b => b.Entity<SampleEntity>().ToSqlQuery("SELECT foobar"));
Assert.Null(entityType.GetTableName());
}
private IEntityType BuildEntityType(Action<ModelBuilder> builderAction, CultureInfo culture = null) private IEntityType BuildEntityType(Action<ModelBuilder> builderAction, CultureInfo culture = null)
=> BuildModel(builderAction, culture).GetEntityTypes().Single(); => BuildModel(builderAction, culture).GetEntityTypes().Single();

View File

@@ -109,6 +109,16 @@ namespace EFCore.NamingConventions.Internal
{ {
var entityType = entityTypeBuilder.Metadata; 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 if (name != RelationalAnnotationNames.TableName
|| StoreObjectIdentifier.Create(entityType, StoreObjectType.Table) is not StoreObjectIdentifier tableIdentifier) || StoreObjectIdentifier.Create(entityType, StoreObjectType.Table) is not StoreObjectIdentifier tableIdentifier)
{ {