C
C#

help

EFCore Updating an item after mapping with Automapper

WWarChortle9/28/2022
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 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 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);
}
}
public TranslatedItem : ReactiveObject
{
private string _name;
public string Name
{
get => _name;
set => this.RaiseAndSetIfChanged(ref _name, value);
}
}
YYawnder9/28/2022
I wouldn't do that. It can work, but it's usually more trouble to use mappers than to map it manually.

Looking for more? Join the community!