1

Hello i am using in an Asp.net MVC CORE project a repository pattern adapted from here. while i am using the GetAll() method to make a call to the database works if i try to use

T GetSingle(object id);
T GetSingle(Expression<Func<T, bool>> predicate);

like this var model = _repository.GetSingle(x => x.Id == id);

or like this var model = _repository.GetSingle(id);

Model returns Null.

in the Razor View if i change from @model IEnumerable<ApplicationUser> which works ok with the GetAll()

to this @model ApplicationUser i get Null. My controller is like this:

[HttpGet]
public IActionResult Index(string id)
{
    //var model = _repository.GetAll();
    var model = _repository.GetSingle(x => x.Id == id);
    //var model = _repository.GetSingle(id);
    if(model == null)
    {
        return NotFound();
    }
    return View(model);
}

Here is the declaration of the Repository if it helps:

public class EntityBaseRepository<T> : IRepository<T> where T : class, new() 

i am wondering why database returns Null with those two methods??

these are the methods

        public T GetSingle(object id)
        {
            return _context.Set<T>().Find(id);
        }
        public T GetSingle(Expression<Func<T, bool>> predicate)
        {
            return _context.Set<T>().FirstOrDefault(predicate);
        }
            public IEnumerable<T> GetAll()
        {
            return _context.Set<T>().AsEnumerable();
        }

5
  • 1
    What do the GetSingle methods in the repository look like? Commented Dec 9, 2019 at 17:47
  • 5
    Yes, we need to see the actual code for GetSingle in order to tell you why it might not be working. That said, you shouldn't use the repository pattern with EF. It already implements this pattern, and all this code here is just more things to maintain and test for no benefit whatsoever. Inject your context and use that directly, or use a real abstraction pattern like CQRS, service layer, or microservices. Commented Dec 9, 2019 at 17:52
  • So repository pattern is useless in EF?? any good links to implement CQRS or service layer? Commented Dec 9, 2019 at 17:55
  • Does an entity with the given id exist? Commented Dec 9, 2019 at 17:56
  • yes it exists as i said. When using the GetAll() method i manage to get the Id. if i try to get a single entity it returns null. Commented Dec 9, 2019 at 17:57

1 Answer 1

1

Please, try to rewrite your GetSingle method like this:

public T GetSingle(Expression<Func<T, bool>> predicate)
{
    return _context.Set<T>().Where(predicate).FirstOrDefault();
}

It can helps you, but I am not shure. So, using AsEnumerable method is bad practise. When you call it, all data from database will fetched and parsed to objects. If you has thousands of rows in database, you give a OutOfMemory exception

Sign up to request clarification or add additional context in comments.

3 Comments

To understand it you should read about IEnumerable and IQueryble. For example here codeproject.com/Articles/732425/IEnumerable-Vs-IQueryable
In your case _context.Set<T>() is IQueryable and when you call Where it will convert it to SQL request. But FirstOrDefault can not do it. This method is work with fetched data.
One more point is using of Expression<Func<T, bool>> predicate. Not Func, it is Expression. And it is Important. Due to Func is compiled Expression. When you call method Where it takes Expression and compile it to SQL request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.