I have a DB-First approach in the project, so i have to do some manual EF mappings. Is there any way for EF Value Conversion to do a conversion for multiple columns into one record? All the examples in the documentation are for one property only. https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions?tabs=data-annotations
Items Table:
Id | ITM_Price | ITM_Currency | ITM_Name |
---|---|---|---|
1 | 420 | USD | Item name |
Example:
public class Item
{
public long Id { get; set; }
public string Name { get; set; }
public Price Price { get; set; }
}
public record Price(decimal Value, string Currency);
The current EntityTypeBuilder is for the Item
.
builder
.Property(e => e.Name)
.HasColumnName("ITM_Name");
builder
.Property(e => e.Price)
.HasColumnName("ITM_Price")
.HasColumnType("decimal(25, 6)")
.HasConversion(
v => v,
v => new Price(v, ???)); // Can't set the currency via constructor / property initialization;
I'd like to keep the configuration in the config for Item
mapping two columns into this record but if it's not possible do I require to create a seperate config for Price?
CodePudding user response:
Found a solution for that.
Instead of using conversion, the Owned Entity Types are a good match to solve that issue: