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

BulkInsertAsync on Postgre, OwnsOne entity and Enum fails #1108

Closed
osjimenez opened this issue Feb 24, 2023 · 1 comment
Closed

BulkInsertAsync on Postgre, OwnsOne entity and Enum fails #1108

osjimenez opened this issue Feb 24, 2023 · 1 comment
Labels

Comments

@osjimenez
Copy link

osjimenez commented Feb 24, 2023

I have this scenario:

public class TimeRecord
	...

	public required TimeRecordSource Source { get; set; }
}
public class TimeRecordSource
{
	public required TimeRecordSourceType Type { get; set; }
	public string? Name { get; set; }
	public string? Id { get; set; }
}
public enum TimeRecordSourceType
{
	Unknown = 0,
	Device,
	Operator,
}

With this configuration:

public class TimeRecordConfiguration : IEntityTypeConfiguration<TimeRecord>
{
	public void Configure(EntityTypeBuilder<TimeRecord> builder)
	{
		...
		builder.OwnsOne(typeof(TimeRecordSource), nameof(TimeRecord.Source));
	}
}

When I'm trying a BulkInsertAsync, I receive this error:
Can't write CLR type Namespace.TimeRecordSourceType with handler type Int32Handler

Originally, I had this configuration:

builder.OwnsOne(typeof(TimeRecordSource), nameof(TimeRecord.Source)).Property(nameof(TimeRecordSource.Type)).HasConversion<string>().HasMaxLength(50);

But failed too with this error:
Can't write CLR type Namespace.TimeRecordSourceType with handler type String

How can I do this scenario to work?

@borisdj
Copy link
Owner

borisdj commented Apr 19, 2023

On PG for this case Converter needs to be explicitly configured in OnModelCreating:

modelBuilder.Entity<TimeRecord>().OwnsOne(a => a.Source,
  b => b.Property(p => p.Type).HasConversion(new EnumToNumberConverter<TimeRecordSourceType, int>()));

@borisdj borisdj closed this as completed Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants