I have the following Entity Framework Entities:
public class Region
{
public int RegionId { get; set; } // Primary Key
public string Name { get; set; }
public virtual ICollection<Country> Countries { get; set; } // Link Table
}
public class Country
{
public int CountryId { get; set; } // Primary Key
public string Name { get; set; }
public int RegionId { get; set; } // Foreign Key
}
I map these using AutoMapper to the following ViewModels:
public class RegionViewModel
{
public int RegionId { get; set; }
public string Name { get; set; }
public virtual ICollection<int> Countries { get; set; }
}
public class CountryViewModel
{
public int CountryId { get; set; }
public string Name { get; set; }
}
I want to translate my ViewModels to Entities using AutoMapper so I can save a new Region. This is my mapping code:
Mapper.CreateMap<RegionViewModel, Region>()
.ForMember(x => x.Countries, x => x.MapFrom(y => y.Countries.Select(z => new Country() { CountryId = z }).ToArray()));
This causes an exception when adding the region in the repository as it also tries to create a new instance of Country with a null Name. One solution is to change the Add
method in my repository to set the State of the country objects to Unchanged.
public async Task Add(Region region)
{
foreach (Country country in region.Countries)
{
this.Context.Entry(country).State = EntityState.Unchanged;
}
await base.Add(region);
}
The other alternative solution is to use more complicated translation logic which uses another repository to get the real country objects. This approach has slower performance because it has to make an extra call to the database but you also get a more complete Region object.
Mapper.CreateMap<RegionViewModel, Region>();
Mapper.CreateMap<int[], Country[]>().ConvertUsing(x => countryRepository.GetAll().Result.Where(y => x.Contains(y.CountryId)).ToArray());
I lean to the first one but what is the correct approach?