efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Enum types with uppercase letters not working

Open quan01994 opened this issue 8 years ago • 12 comments

PostgreSQL install on windows.

image

in my context

static PgSqlDbContext()
 { 
     NpgsqlConnection.GlobalTypeMapper.MapEnum<Mood>("Mood");
  }

 protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
            modelBuilder.ForNpgsqlHasEnum("Mood", new[] { "happy", "sad" });

            modelBuilder.Entity<News>().ForNpgsqlHasComment("测试新闻类的注释");

            modelBuilder.Entity<News>().ForNpgsqlUseXminAsConcurrencyToken();

            base.OnModelCreating(modelBuilder);
}

my model

public class News
    {
        [Key]

        public int NewsId { get; set; }
        
        [StringLength(200)]
        public string Titile { get; set; }
        
        public Instant CrateTime { get; set; }
        
        public string[] Tags { get; set; }
        
        public List<string> AlternativeTags { get; set; }
        
        public Mood Mood { get; set; }
    }


    public enum Mood
    {
        [PgName("happy")]
        Happy,

        [PgName("sad")]
        Sad,
    }

tim 20180531150339

quan01994 avatar May 31 '18 07:05 quan01994

Can you please post the querying C# code?

roji avatar May 31 '18 07:05 roji

 using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
                var context = serviceScope.ServiceProvider.GetRequiredService<PgSqlDbContext>();
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();  //throw this exception
}

@roji

when i create database ,throw this exception

quan01994 avatar May 31 '18 07:05 quan01994

image

quan01994 avatar May 31 '18 07:05 quan01994

modelBuilder.ForNpgsqlHasEnum("mood", new[] { "happy", "sad" });

"happy", "sad" must be lowercase

if i use uppercase and my enum

public enum Mood
    {
        Happy,

        Sad,
    }

throw another exception

quan01994 avatar May 31 '18 07:05 quan01994

This is because of #396...

Type names are like other identifiers - if unquoted they are folded to lowercase by PostgreSQL. The problem is that there are people out there specifying the column types as uppercase TEXT, and adding quotes would break their code.

The fix is to only ever quote only user-defined type names (enums, composites in the future), continuing to allow other types to be specified in lower/upper case etc.

roji avatar Jun 10 '18 18:06 roji

@roji but on http://www.npgsql.org/efcore/mapping/enum.html
Still using uppercase,

quan01994 avatar Jun 11 '18 07:06 quan01994

@quan01994 I don't understand your comment...

I pushed a fix that will allow you to use uppercase enum type names - this will be released in 4.0.1. Until that comes out you have to use lowercase names. I don't see any reason to change the docs since the uppercase issue is fixed...

roji avatar Jun 11 '18 08:06 roji

ok,but in 4.0.0 i need use lowercase ,in next version ,i can use uppercase?

quan01994 avatar Jun 12 '18 05:06 quan01994

You can use lowercase-only enum names in 4.0.0, but using any uppercase letters will cause a failure. This is fixed in 4.0.1, where you'll be able to use any name you want, uppercase or lowercase.

roji avatar Jun 12 '18 06:06 roji

thanks 👍 : )

quan01994 avatar Jun 12 '18 07:06 quan01994

Also worth noting that an enum defined: public Enum Mood{ "Happy", "NotHappy" } must be mapped...

modelBuilder.ForNpgsqlHasEnum("mood", new[] { "happy", "not_happy" });

Is this the expected behavior?

jnb-source avatar Jun 27 '18 08:06 jnb-source

@JNBarnes you're referring to the fact that the .NET enum has Happy and NotHappy, while the PostgreSQL enum needs to have happy and not_happy? If so, you need to read about name translation in the docs.

roji avatar Jun 27 '18 17:06 roji