I have the following domain models:
public class Country
{
public string ISOAlpha3Code { get; set; }
public string NameUS { get; set; }
public string NameES { get; set; }
public string NameFR { get; set; }
public List<Province> Provinces { get; set; }
}
public class Province
{
public string Code { get; set; }
public string NameUS { get; set; }
public string NameES { get; set; }
public string NameFR { get; set; }
}
The database view that has to be used to populate these models returns a flat list, like this:
I was hoping to configure the entity, for the DbContext, to split out provinces for the countries.
I could create a domain model that matches the database results, though I would really like to separate the domain model from how its represented in this particular database - especially if the DB changes.
Is something like this possible? And If so, how would it be implemented?
public class CountryConfiguration : IEntityTypeConfiguration<Country>
{
public void Configure(EntityTypeBuilder<Country> builder)
{
builder.ToTable("v_WS_CountriesAndProvinces");
builder.Property(p => p.NameUS).HasColumnName("CountryNameUS");
builder.Property(p => p.NameES).HasColumnName("CountryNameES");
builder.Property(p => p.NameFR).HasColumnName("CountryNameFR");
// something to split out the provinces?
builder.Property(p => p.Provinces).HasConversion(
v => ToDB(v), // to DB
v => FromDB(v) // from DB
);
}
private List<Province> FromDB(object v)
{
throw new NotImplementedException();
}
private object ToDB(List<Province> v)
{
throw new NotImplementedException();
}
}