N.EntityFrameworkCore.Extensions icon indicating copy to clipboard operation
N.EntityFrameworkCore.Extensions copied to clipboard

Mapping with value converters(HasConversion) does not work

Open osmozis opened this issue 1 year ago • 1 comments

To Reproduce

  1. create table in database
CREATE SCHEMA [scm];
GO

CREATE TABLE [scm].[tbl](
	[id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[name] [nvarchar](120) NULL,
	[color] [int] NULL,
) ON [PRIMARY]
GO
  1. define the class
class Item
{
    public int Id {get;set;} 
    public string Name {get;set;}
    public System.Drawing.Color Color {get;set;}
}
  1. override DbContext's OnModelCreating
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Item>(f =>
    {
        f.ToTable("tbl", "scm");

        f.HasKey(x => x.Id);
        f.Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
        f.Property(x => x.Name);
        f.Property(x => x.Color).HasConversion(x => x.ToArgb(), x => Color.FromArgb(x));
    });
}
  1. test method
var items = new List<Item>();

for (var i = 1; i <= 1_000; ++i)
{
    var item = new Item
    {
        Id = i,
        Name = i.ToString(),
        Color = Color.FromArgb(i)
    };

    items.Add(item);
}

context.BulkInsert(items);
context.BulkSaveChanges();

Expected behavior item.Color property should be converted to int before sending to database

osmozis avatar May 13 '24 07:05 osmozis

osmozis,

This issue was addressed in v8.0.0.9

NorthernLight1 avatar May 15 '24 22:05 NorthernLight1