Return an entity

1

I'm working with Entity Framewor app windowsw forms, I need to return an enbity for that I'm calling the Single method but I do not know how to use it

What I try to implement if it exists returns an entity and does not exist, it returns a null, in other words if it exists, it returns the entity and I update it and if it does not exist it returns null and I create.

public void Create(Talla entity)
    {
        if(_tallaRepository.Single()
        //if (_tallaRepository.Existe(entity.TallaId))
            _tallaRepository.Update(entity);
        else
            _tallaRepository.Create(entity);
    }

This method returns an entity instead of true or false

public bool Existe(int codigo)
    {
        int result = Context.Tallas.Where(x => x.TallaId == codigo).Count();
        if (result == 0)
            return false;
        else
            return true;
    }
    
asked by Pedro Ávila 07.06.2016 в 19:43
source

1 answer

1

But to validate if you do not use the Single () you have to create a new method that uses the Any () of linq

public class RepositoryBase
{
    public bool Exist(Expresion<Func<T, bool> predicate)
    {
        return contexto.Any(predicate);
    }
}

The idea is that you define something like being

if(_tallaRepository.Exist(x=> x.Id == entity.Id)){
   //aqui actualizas
}

You must define the predicate as valid if the entity exists, the id is used for this

    
answered by 07.06.2016 / 19:53
source