-
-
Notifications
You must be signed in to change notification settings - Fork 307
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
Comments
@omccully I am not able to reproduce this, please provide full repro details. A DbContext is not in the System namespace... |
@omccully Ping? |
Hey Erik, I have the same problem, where the Db Context Namespace (eg I'm avoiding the issue by including manually appending Here is my {
"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 // <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 // <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; }
} |
Coul |
@alexhelvetica Could you share your CREATE TABLE statement also? |
@ErikEJ of course! I assume you want the SQL, I'll send that tomorrow morning. |
@alexhelvetica Yes, please, then I can attempt a repro |
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 |
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
}
] |
@Mattygs6 Thanks, seems like a proper solution |
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
The text was updated successfully, but these errors were encountered: