C#C
C#4y ago
Warchortle

EFCore Updating an item after mapping with Automapper

Hey I am using Ef core to store some data in sqlite. I need to translate it to another object to show it in my UI. When I go to update it, it is adding another copy of the item to the DB. How can I transform between the two items and update or add as needed.

How I am doing the update

public void UpsertAsync(TranslatedItem message)
{
    var result = _mapper.Map<EfItem>(message);
    if (_appDataContext.EnvItems.Any(x => x.Name == result.Name))
    {
        _appDataContext.EfItems.Update(result);
    }
    else
    {
        _appDataContext.EfItems.Add(result);
    }
    _appDataContext.SaveChanges();
}


public EfItem
{
  public int Id {get; set;}
  public string Name {get; set;}
}


public TranslatedItem : ReactiveObject
{
    private string _name;
    public string Name
    {
        get => _name;
            set => this.RaiseAndSetIfChanged(ref _name, value);
    }
}
Was this page helpful?