Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC

Activities of "virtual"

Thank you for replying. I see what you mean, I recently just queried my database to restore references and mapped manually from dtos. I understand that automapper isn't designed to map dtos back to entities. But isn't there a more elegant solution?

ok, so I tried to update the tables like this:

public async Task InsertOrUpdateRecipe(GetRecipeInput input)
        {
            try
            {

                if (input != null && input.Id != 0)
                {
                    var recipe = await _recipeRepository.GetAsync(input.Id);
                    var mapped = input.MapTo(recipe);
                    
                    await _recipeRepository.UpdateAsync(mapped);
                    _unitOfWorkManager.Current.SaveChanges();
                }
                else if (input != null)
                {
                    await _recipeRepository.InsertAsync(input.MapTo<Recipe>());
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

And the exception is thrown right after saving changes:

Exception thrown: 'System.InvalidOperationException' in EntityFramework.dll

Additional information: 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.

Anybody experienced anything like that?

Showing 1 to 2 of 2 entries