Skip to Main Content

ODP.NET

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

Default value for non-unicode column is generated with N prefix when using a value converter

user-yder5Jun 23 2023

I have an entity with property of enum type (Product.Type in my example).
In entity configuration that property is configured as unicode = false column with enum to string conversion with default value of one of the options of that enum (ProductType.Other in example).

The problem is that default value of Product.Type generated as unicode string (N'Other'), while the column is non-unicode.

using Microsoft.EntityFrameworkCore;
public class ProductContext : DbContext
{
   public DbSet<Product> Products { get; set; }
   // The following configures EF to create a SqlServer database file in the
   // special "local" folder for your platform.
   protected override void OnConfiguring(DbContextOptionsBuilder options)
       => options.UseOracle($"Hidden connection string");
   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
       modelBuilder.Entity<Product>().Property(p => p.Type)
           .IsUnicode(false)
           .HasConversion(v => v.ToString(), v => Enum.Parse<ProductType>(v))
           .HasDefaultValue(ProductType.Other);
       base.OnModelCreating(modelBuilder);
   }
}
public class Product
{
   public long Id { get; set; }
   public string Name { get; set; }
   public ProductType Type { get; set; }
}
public enum ProductType
{
   Book,
   Toy,
   Other
}

For testing purpose I created separate migration with adding of Product.Type property and tried to generate migration script.

The generated script is:

ALTER TABLE "Products" ADD "Type" varchar(max) DEFAULT N'Other' NOT NULL
/
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES (N'20230623134857_AddedProductType', N'7.0.8')
/

Also generated the script using SqlServer provider, N prefix is not present there:

BEGIN TRANSACTION;
GO
ALTER TABLE [Products] ADD [Type] varchar(max) NOT NULL DEFAULT 'Other';
GO
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20230623134857_AddedProductType', N'7.0.8');
GO
COMMIT;
GO

Also tried to implement custom converter and use ConverterMappingHints with unicode = false option - the same result.

Do you have any ideas how to configure non-unicode column with enum-to-string converter to have default value generated as non-unicode?

This post has been answered by Alex Keh-Oracle on Mar 12 2024
Jump to Answer
Comments
Post Details
Added on Jun 23 2023
6 comments
472 views