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

Migrations error caused by missing enum type #1388

Closed
MoritzMK opened this issue May 25, 2020 · 2 comments
Closed

Migrations error caused by missing enum type #1388

MoritzMK opened this issue May 25, 2020 · 2 comments

Comments

@MoritzMK
Copy link

Hi,
I try to create my database with migrations. My database context is a separate class library. When I call

dotnet ef database update --startup-project ../SpecialInspection/

I get the following error:

42704: type "inspection_value_type" does not exist

Here is my code:

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

        public DbSet<InspectionValue> InspectionValues { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            NpgsqlConnection.GlobalTypeMapper.MapEnum<InspectionValueType>();
        }

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

            modelBuilder.HasPostgresEnum<InspectionValueType>();

            modelBuilder.Entity<InspectionValue>().Property(i => i.Id).UseIdentityAlwaysColumn();
            modelBuilder.Entity<InspectionValue>().HasIndex(i => new {i.Type, i.Value}).IsUnique();
        }
    }
    public enum InspectionValueType
    {
        Customer,

        LockingSystem,

        Article,

        Order
    }
        public InspectionValue(string value, InspectionValueType type, DateTime createdAt, string createdBy)
        {
            Value = value;
            Type = type;
            CreatedAt = createdAt;
            CreatedBy = createdBy;
        }

        public InspectionValue(int id, string value, InspectionValueType type, DateTime createdAt, string createdBy)
        {
            Id = id;
            Value = value;
            Type = type;
            CreatedAt = createdAt;
            CreatedBy = createdBy;
        }

        [Key]
        public int? Id { get; set; }
        
        [Required]
        public string Value { get; set; }

        [Required]
        public InspectionValueType Type { get; set; }

        [Required]
        public DateTime CreatedAt { get; set; }

        [Required]
        public string CreatedBy { get; set; }
    }
    public class DesignTimeContextFactory : IDesignTimeDbContextFactory<SpecialInspectionContext>
    {
        public SpecialInspectionContext CreateDbContext(string[] args)
        {
            var basePath = Directory.GetCurrentDirectory();

            var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            var configuration = new ConfigurationBuilder()
                .SetBasePath(basePath)
                .AddJsonFile($"{basePath}/../SpecialInspection/appsettings.json")
                .AddJsonFile($"{basePath}/../SpecialInspection/appsettings.{environmentName}.json", true)
                .Build();

            var builder = new DbContextOptionsBuilder<SpecialInspectionContext>().UseNpgsql(configuration.GetConnectionString("Postgres"));

            return new SpecialInspectionContext(builder.Options);
        }
    }
@roji
Copy link
Member

roji commented May 25, 2020

Duplicate of #930

@roji roji marked this as a duplicate of #930 May 25, 2020
@roji
Copy link
Member

roji commented May 25, 2020

We unfortunately have some issues with enums created in non-default schemas. As a workaround, you can edit the generated SQL migration script and add the schema, or specifically create the enum in the public schema.

@roji roji closed this as completed May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants