© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
1 reply
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 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);
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

AutoMapper.Collection.EFCore
C#CC# / help
4y ago
❔ AutoMapper Mapping Profiles Configuration
C#CC# / help
3y ago
AutoMapper example projects with mapping profiles
C#CC# / help
11mo ago
✅ AutoMapper - mapping record to a class (.NET 7)
C#CC# / help
3y ago