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

Tables with names the same as other classes within the System namespace cause uncompilable code #2728

Closed
omccully opened this issue Dec 13, 2024 · 10 comments
Labels
question Further information is requested

Comments

@omccully
Copy link

omccully commented Dec 13, 2024

Another bug that is an uncommon edge case I encountered due to working with an old database.

Provide steps to reproduce a bug

With Microsoft SQL Server, add a table named "Type" or "Action". Run the scaffolding. The code does not compile because it conflicts with System.Type or System.Action. The compile errors are in the db context class.

I'm using "use-schema-folders-preview": true and "use-database-names": true.

Perhaps it could just fully qualify all model type names in the generated db context code.

Provide technical details

  • EF Core Power Tools version: 9.1.624 CLI

  • Exact Visual Studio version: 2022, 17.10.4

  • Database engine: SQL Server

  • EF Core version in use: EF Core 9

  • Is Handlebars templates used: no

  • Is T4 templates used: no

  • Is a SQL Server .dacpac used: no

@omccully omccully changed the title Tables with names similar to other classes within the System namespace cause uncompilable code Tables with names the same as other classes within the System namespace cause uncompilable code Dec 13, 2024
@ErikEJ ErikEJ added bug Something isn't working reveng reveng-cli labels Dec 13, 2024
@ErikEJ
Copy link
Owner

ErikEJ commented Dec 14, 2024

@omccully I am not able to reproduce this, please provide full repro details.

A DbContext is not in the System namespace...

@ErikEJ ErikEJ added question Further information is requested and removed bug Something isn't working reveng reveng-cli labels Dec 14, 2024
@ErikEJ
Copy link
Owner

ErikEJ commented Dec 17, 2024

@omccully Ping?

@ErikEJ ErikEJ closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2024
@alexhelvetica
Copy link

alexhelvetica commented Jan 30, 2025

@omccully I am not able to reproduce this, please provide full repro details.

A DbContext is not in the System namespace...

Hey Erik, I have the same problem, where the Db Context Namespace (eg db) is different from the Entity Types Namespace. (eg db.models)

I'm avoiding the issue by including manually appending using Type = ef.mow.Models.Type; in the DB Context, but if there's a way to configure EF Core Power Tools to fully qualify the Entity Types in the Context, that would be amazing.

Here is my efpt.mow.config.json

{
   "CodeGenerationMode": 5,
   "ContextClassName": "mowContext",
   "ContextNamespace": null,
   "FilterSchemas": false,
   "IncludeConnectionString": false,
   "ModelNamespace": null,
   "OutputContextPath": "mow",
   "OutputPath": "mow\/Models",
   "PreserveCasingWithRegex": true,
   "ProjectRootNamespace": "ef",
   "Schemas": null,
   "SelectedHandlebarsLanguage": 2,
   "SelectedToBeGenerated": 0,
   "T4TemplatePath": null,
   "Tables": [
      {
         "Name": "[dbo].[Types]",
         "ObjectType": 0
      }
   ],
   "UiHint": null,
   "UncountableWords": null,
   "UseAsyncStoredProcedureCalls": true,
   "UseBoolPropertiesWithoutDefaultSql": false,
   "UseDatabaseNames": false,
   "UseDateOnlyTimeOnly": false,
   "UseDbContextSplitting": false,
   "UseDecimalDataAnnotationForSprocResult": true,
   "UseFluentApiOnly": true,
   "UseHandleBars": false,
   "UseHierarchyId": false,
   "UseInflector": true,
   "UseLegacyPluralizer": false,
   "UseManyToManyEntity": false,
   "UseNoDefaultConstructor": false,
   "UseNoNavigations": false,
   "UseNoObjectFilter": false,
   "UseNodaTime": false,
   "UseNullableReferences": false,
   "UsePrefixNavigationNaming": false,
   "UseSchemaFolders": false,
   "UseSchemaNamespaces": false,
   "UseSpatial": false,
   "UseT4": false
}

And the DB Context mowContext.cs

// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;
using ef.mow.Models;
using Microsoft.EntityFrameworkCore;

namespace ef.mow;

public partial class mowContext : DbContext
{
    public mowContext(DbContextOptions<mowContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Type> Types { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.UseCollation("SQL_Latin1_General_CP1_CI_AS");

        modelBuilder.Entity<Type>(entity =>
        {
            entity.HasKey(e => e.Type1);

            entity.Property(e => e.Type1)
                .ValueGeneratedNever()
                .HasColumnName("Type");
            entity.Property(e => e.Description).HasMaxLength(50);
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

And Entity Type Type Type.cs

// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;

namespace ef.mow.Models;

public partial class Type
{
    public short Type1 { get; set; }

    public string Description { get; set; }

    public short? Priority { get; set; }
}

@ErikEJ
Copy link
Owner

ErikEJ commented Jan 30, 2025

Coul

@ErikEJ ErikEJ reopened this Jan 30, 2025
@ErikEJ
Copy link
Owner

ErikEJ commented Jan 30, 2025

@alexhelvetica Could you share your CREATE TABLE statement also?

@alexhelvetica
Copy link

@ErikEJ of course! I assume you want the SQL, I'll send that tomorrow morning.

@ErikEJ
Copy link
Owner

ErikEJ commented Jan 30, 2025

@alexhelvetica Yes, please, then I can attempt a repro

@alexhelvetica
Copy link

Thanks @ErikEJ. The DB is running MSSQL Server 14.0.2070.1

USE [mow]
GO
/****** Object:  Table [dbo].[Types]    Script Date: 04/11/2024 9:44:14 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Types](
	[Type] [smallint] NOT NULL,
	[Description] [nvarchar](50) NULL,
	[Priority] [smallint] NULL,
 CONSTRAINT [PK_Types] PRIMARY KEY CLUSTERED 
(
	[Type] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

@Mattygs6
Copy link

Mattygs6 commented Feb 1, 2025

This can be resolved by renaming. It is a much better solution than having to force namespaces and usings everywhere you use it in code. make an efpt.mow.renaming.json file like below

[
   {
      "SchemaName": "dbo",
      "Tables": [
         {
            "Name": "Type",
            "NewName": "TypeEntity"
         }
      ],
      "UseSchemaName": false
   }
]

@ErikEJ
Copy link
Owner

ErikEJ commented Feb 2, 2025

@Mattygs6 Thanks, seems like a proper solution

@ErikEJ ErikEJ closed this as not planned Won't fix, can't repro, duplicate, stale Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants