I’m trying to map a complex type as described in Value objects using Complex Types.
using System.ComponentModel.DataAnnotations.Schema;
public record Foo
{
public Bar Bar { get; set; }
}
[ComplexType]
public record Bar
{
public int X { get; set; }
public int Y { get; set; }
}
I expected this to map to two integer columns in the Foo
table, Bar_X
and Bar_Y
. Instead it produces an exception: The exception 'The 'Foo' property 'Foo.Bar' could not be mapped because the database provider does not support this type. Consider converting the property value to a type supported by the database using a value converter. See https://aka.ms/efcore-docs-value-converters for more information. Alternately, exclude the property from the model using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'
Am I missing something here? I have the following in my service configuration:
builder.Services.AddDbContext<DataContext>(opt => opt.UseInMemoryDatabase("Data"));
I’ve also tried with the Postgres database provider:
builder.Services.AddDbContext<DataContext>(opt => opt.UseNpgsql(
builder.Configuration.GetConnectionString("DefaultConnection")
));
I’ve also tried adding [Required]
per a tutorial at https://www.learnentityframeworkcore.com/model/complex-type, but that didn’t change anything.