I trying to update nested collection (Cities) of Country entity.
Just simple enitities and dto's:
// EF Models
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public int CountryId { get; set; }
public int? Population { get; set; }
public virtual Country Country { get; set; }
}
// DTo's
public class CountryData : IDTO
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<CityData> Cities { get; set; }
}
public class CityData : IDTO
{
public int Id { get; set; }
public string Name { get; set; }
public int CountryId { get; set; }
public int? Population { get; set; }
}
And code itself (tested in console app for the sake of simplicity):
using (var context = new Context())
{
// getting entity from db, reflect it to dto
var countryDTO = context.Countries.FirstOrDefault(x => x.Id == 1).ToDTO<CountryData>();
// add new city to dto
countryDTO.Cities.Add(new CityData
{
CountryId = countryDTO.Id,
Name = "new city",
Population = 100000
});
// change existing city name
countryDTO.Cities.FirstOrDefault(x => x.Id == 4).Name = "another name";
// retrieving original entity from db
var country = context.Countries.FirstOrDefault(x => x.Id == 1);
// mapping
AutoMapper.Mapper.Map(countryDTO, country);
// save and expecting ef to recognize changes
context.SaveChanges();
}
This code throws exception:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
even though entity after last mapping seems just fine and reflects all changes properly.
I've spent much time finding solution but got no result. Please help.
The problem is the country
you are retrieving from database already has some cities. When you use AutoMapper like this:
// mapping
AutoMapper.Mapper.Map(countryDTO, country);
AutoMapper is doing something like creating an IColletion<City>
correctly (with one city in your example), and assigning this brand new collection to your country.Cities
property.
The problem is EntityFramework doesn't know what to do with the old collection of cities.
In fact, EF cannot decide for you. If you want to keep using AutoMapper, you can customize your mapping like this:
// AutoMapper Profile
public class MyProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<CountryData, Country>()
.ForMember(d => d.Cities, opt => opt.Ignore())
.AfterMap(AddOrUpdateCities);
}
private void AddOrUpdateCities(CountryData dto, Country country)
{
foreach (var cityDTO in dto.Cities)
{
if (cityDTO.Id == 0)
{
country.Cities.Add(Mapper.Map<City>(cityDTO));
}
else
{
Mapper.Map(cityDTO, country.Cities.SingleOrDefault(c => c.Id == cityDTO.Id));
}
}
}
}
The Ignore()
configuration used for Cities
makes AutoMapper just keep the original proxy reference built by EntityFramework
.
Then we just use AfterMap()
to invoke an action doing exactly what you thougth:
Map
where we pass the existing entity as the second parameter, and the city proxy as first parameter, so automapper just updates the existing entity's properties.Then you can keep your original code:
using (var context = new Context())
{
// getting entity from db, reflect it to dto
var countryDTO = context.Countries.FirstOrDefault(x => x.Id == 1).ToDTO<CountryData>();
// add new city to dto
countryDTO.Cities.Add(new CityData
{
CountryId = countryDTO.Id,
Name = "new city",
Population = 100000
});
// change existing city name
countryDTO.Cities.FirstOrDefault(x => x.Id == 4).Name = "another name";
// retrieving original entity from db
var country = context.Countries.FirstOrDefault(x => x.Id == 1);
// mapping
AutoMapper.Mapper.Map(countryDTO, country);
// save and expecting ef to recognize changes
context.SaveChanges();
}
This is not an answer per se to the OP, but anyone looking at a similar problem today should consider using AutoMapper.Collection. It provides support for these parent-child collection issues that used to require a lot of code to handle.
I apologize for not including a good solution or more detail, but I am only coming up to speed on it now. There is an excellent simple example right in the README.md displayed on the link above.
Using this requires a bit of a rewrite, but it drastically cuts down on the amount of code you have to write, especially if you're using EF and can make use of AutoMapper.Collection.EntityFramework
.