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?
Hello. I am trying to update my records but only the table I am updating with UpdateAsync will update. I use AutoMapper to map to DTOs, the fields seem to map alright. The insertion is working and I can see data in all three tables. So, I am trying to update my recipeRepository, but only the recipe table is being updated entity classes:
public class Recipe : Entity, ISoftDelete, IHasCreationTime, IHasModificationTime
{
[Key]
[Column("id")]
public override int Id { get; set; }
public string recipe_name { get; set; }
public string recipe_dish { get; set; }
public Nullable<float> recipe_cooking_time { get; set; }
public Nullable<float> recipe_preparation_time { get; set; }
public string recipe_author { get; set; }
public string recipe_description { get; set; }
public string recipe_image { get; set; }
public virtual ICollection<Ingredient> Ingredients { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreationTime { get; set; }
public DateTime? LastModificationTime { get; set; }
}
}
public class Product : Entity
{
[Key]
[Column("id")]
public override int Id { get; set; }
public string product_name { get; set; }
public virtual ICollection<Ingredient> Ingredients { get; set; }
}
public class Ingredient : Entity
{
[Key]
[Column("id")]
public override int Id { get; set; }
public Nullable<float> ingredient_quantity { get; set; }
public string ingredient_unit { get; set; }
public string ingredient_notes { get; set; }
[Required]
public int ingredient_recipe_id { get; set; }
[Required]
public int ingredient_product_id { get; set; }
[ForeignKey("ingredient_product_id")]
public virtual Product Product { get; set; }
[ForeignKey("ingredient_recipe_id")]
public virtual Recipe Recipe { get; set; }
}
}
I am using mysql provider, maybe it's the provider's fault?