mirror of
https://github.com/fergalmoran/EFCore.NamingConventions.git
synced 2025-12-22 01:28:13 +00:00
Don't create table for View/SqlQuery/Function-mapped entity types (#104)
Fixes #94
This commit is contained in:
@@ -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<SampleEntity>().ToSqlQuery("SELECT foobar"));
|
||||
|
||||
Assert.Null(entityType.GetTableName());
|
||||
}
|
||||
|
||||
private IEntityType BuildEntityType(Action<ModelBuilder> builderAction, CultureInfo culture = null)
|
||||
=> BuildModel(builderAction, culture).GetEntityTypes().Single();
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user