Skip to content

Mapping issues with schema-scoped enums #930

@ZekeLu

Description

@ZekeLu

According to what I have read, the original issue #554 has been addressed by the pull request #605. But it seems that this issue still exists in the latest preview (3.0.0-preview5).

Here is the model and configuration:

public enum Mood
{
    Happy,
    Sad
}

public class Blog
{
    public int BlogId { get; set; }
    public Mood Mood { get; set; }
}

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    // map enum without specifying pgName
    static BloggingContext() => NpgsqlConnection.GlobalTypeMapper.MapEnum<Mood>();

    // it works when specified a correct pgName
    // static BloggingContext() => NpgsqlConnection.GlobalTypeMapper.MapEnum<Mood>("my.mood");

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseNpgsql("Host=my_host;Database=my_db;Username=my_user;Password=my_pw");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // specify a default schema
        modelBuilder.HasDefaultSchema("my");
        modelBuilder.ForNpgsqlHasEnum<Mood>();
    }
}

The script generated by dotnet ef migrations script is:

CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
    "MigrationId" character varying(150) NOT NULL,
    "ProductVersion" character varying(32) NOT NULL,
    CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);

CREATE SCHEMA IF NOT EXISTS my;

CREATE SCHEMA IF NOT EXISTS my;

CREATE TYPE my.mood AS ENUM ('happy', 'sad');

CREATE TABLE my."Blogs" (
    "BlogId" serial NOT NULL,
    "Mood" mood NOT NULL, -- ERROR: type "mood" does not exist. should use my.mood
    CONSTRAINT "PK_Blogs" PRIMARY KEY ("BlogId")
);

INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20190718112346_init', '3.0.0-preview5.19227.1');

The expected script to create the my."Blogs" table is:

CREATE TABLE my."Blogs" (
    "BlogId" serial NOT NULL,
    "Mood" my.mood NOT NULL,  -- mood --> my.mood
    CONSTRAINT "PK_Blogs" PRIMARY KEY ("BlogId")
);

And dotnet ef database update will fail with this message:

Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE my."Blogs" (
    "BlogId" serial NOT NULL,
    "Mood" mood NOT NULL,
    CONSTRAINT "PK_Blogs" PRIMARY KEY ("BlogId")
);
Npgsql.PostgresException (0x80004005): 42704: type "mood" does not exist

I have attached the project so that you can examine this issue.

ConsoleApp.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions