0

I have a generic repository method for PUT which works fine and here it finds by the generic method of EF of primary key Id's.
What I am looking for is I want to update a record based on a specific column and its value is of type string. Is there a possibility to do this?

public virtual TEntity GetByID(object id)
{
    return DbSet.Find(id);
}
Sam King
  • 75
  • 1
  • 12
  • [Check the same it will fulfill your requirement ](https://stackoverflow.com/questions/3642371/how-to-update-only-one-field-using-entity-framework) – Pankaj Gupta Oct 10 '17 at 12:32
  • Yes it is possible. So, what have you tried so far? And where exactly are you stuck right now? – bassfader Oct 10 '17 at 12:33
  • Possible duplicate of [generic repository to access database](https://stackoverflow.com/questions/35159068/generic-repository-to-access-database) – Backs Oct 10 '17 at 12:34
  • You said you want to update a record based on a specific column . Does that mean that column will act as a filter to the update ir that only said column will né updated? – Gabriel Rainha Oct 10 '17 at 12:45
  • I tried full entity update `public virtual void Update(TEntity entityToUpdate) { DbSet.Attach(entityToUpdate); Context.Entry(entityToUpdate).State = EntityState.Modified; }` – Sam King Oct 10 '17 at 13:28
  • @GabrielRainha Yes, for example now the update method checks for ID column which is also my primary key column but i have cannot use this column because i need find record based on a value in a specific column and update other 2 columns. – Sam King Oct 10 '17 at 13:31
  • @SamKing Does this specific column change? Will you have to select which column are you looking up to on your method? Or is it aways the same column? – Gabriel Rainha Oct 10 '17 at 13:38
  • @SamKing Also, does ALL your other TEntity classes have the same columns you need to lookup and update? If not, then you are not suposed to implement such method on the generic repository, but rather implement a specific repository for that entity and put the new method there. – Gabriel Rainha Oct 10 '17 at 13:40
  • @GabrielRainha No, the lookup column will be same, Yes the Tentity classes have sames columns. – Sam King Oct 10 '17 at 13:42

1 Answers1

1

Based on what you said on the comments, all you'r entities have the columns you want to lookup and update. Something like this:

public class EntityBase
{
    public string LookupCol { get; set; }
    public string Col1 { get; set; }
    public string Col2 { get; set; }
}

Assuming that is true, you could do the following:

public class GenericRepo<TEntity> where TEntity : EntityBase
{
    ...

    public void UpdateByStringColumn(string lookupCol, string col1Value, string col2Value)
    {
        //if you want it to break in case no entity was found, change it to First and remove the sanity check
        var entity = this.DbSet.FirstOrDefault(p => p.LookupCol == lookupCol); 
        if (entity != null) 
        {
            entity.Col1 = col1Value;
            entity.Col2 = col2Value;
            Context.SaveChanges();
        }
}

}

Gabriel Rainha
  • 1,713
  • 1
  • 21
  • 34