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

Enum with explicitly defined schema the same as the default schema will have its schema removed in ModelSnapshot #1657

Closed
erichiller opened this issue Jan 20, 2021 · 4 comments
Labels
duplicate This issue or pull request already exists

Comments

@erichiller
Copy link

Using EF Core 5 and Npgsql 5, I have several enums defined, and all work as expected, except for enums explicitly set to the public schema (which is the default in this case).

Defined as:
Within my DbContext I setup the enum using

NpgsqlConnection.GlobalTypeMapper.MapEnum<Common.Types.USState>( "public.us_state" );
modelBuilder.HasPostgresEnum<Common.Types.USState>( "public", "us_state" );

I also tried setting HasDefaultSchema('public"); but that didn't help.

before running a migration, XxDBContextModelSnapshot.cs will have:

modelBuilder.HasPostgresEnum("public","us_state", new[] { "unknown", "alabama", "etc..." };

upon creating any migration, even an empty one, XxDBContextModelSnapshot.cs will have the schema removed for that enum:

modelBuilder.HasPostgresEnum("us_state", new[] { "unknown", "alabama", "etc..." };

And then if I do not keep correcting XxDBContextModelSnapshot.cs after any migration is generated I will have all my enums be redefined in the generated migration script. And conflicts will occur for the enum in public as it already exists. Whatever writes out the changes seems to not take default schema into account, and will skip the writing if the schema set explicitly is the same as the default. This might be related to #1027 , but I am not sure.

@roji
Copy link
Member

roji commented Jan 25, 2021

Thanks for reporting, there are definitely some bugs around schema-scoped enums in general.

I'll take a look at this together with #930 and #1315.

@roji roji self-assigned this Jan 25, 2021
@roji roji added the bug Something isn't working label Jan 25, 2021
@roji roji added this to the 6.0.0 milestone Jan 25, 2021
@pedro-sphericode
Copy link

It is possible to trick the migration class by changing the DbContext a little bit.
Let's say that your entities and types (like an enum) must use a schema named "schemaname". You could inform the modelBuilder that a given property will use a specific name, like "schemaname.myenum".

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }

static ApplicationDbContext() => NpgsqlConnection.GlobalTypeMapper.MapEnum<MyEnum>(); // Maybe we could define the schema name here...

public DbSet<MyEntity> MyEntity { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
	modelBuilder.HasDefaultSchema("schemaname");

	modelBuilder.HasPostgresEnum<MyEnum>("schemaname");

	// This solves the problem, but... There must be a better way!
	// Also, it doesn't work if the schema name contains a hyphen (e.g., "schema-name").
	modelBuilder.Entity<MyEntity>()
		.Property(p => p.SomeProperty)
			.HasColumnType("schemaname.myenum"); // This should be managed by EF+Npg

	base.OnModelCreating(modelBuilder);
}	

Note that I had to add an underscore to the enum name. The migration class will be created as it follows:

migrationBuilder.CreateTable(
	name: "MyEntity",
	schema: "schemaname",
	columns: table => new
	{
		Id = table.Column<int>(type: "integer", nullable: false)
			.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
		SomeProperty = table.Column<Constants.MyEnum>(type: "schemaname.my_enum", nullable: false)
	},
	constraints: table =>
	{
		table.PrimaryKey("PK_MyEntity", x => x.Id);
	});

Although it works, I think it would be helpful if the framework could handle it. Also, the presented solution will not help you if your schema name contains a hyphen, as it is going to generate a SQL error when updating your database.

Here is a sample project showing the problem:

https://github.com/please-halp/EFCore5-Npgsql-Issue-using-enum-with-schema

@roji
Copy link
Member

roji commented Oct 26, 2021

Duplicate of #2027

@roji roji marked this as a duplicate of #2027 Oct 26, 2021
@roji roji closed this as completed Oct 26, 2021
@roji
Copy link
Member

roji commented Oct 26, 2021

(fixed for 5.0.11)

@roji roji removed this from the 6.0.0 milestone Oct 26, 2021
@roji roji added duplicate This issue or pull request already exists and removed bug Something isn't working labels Oct 26, 2021
@roji roji removed their assignment Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants