Base solution for your next web application
Open Closed

IRepository throws exception instead of return null #2370


User avatar
0
okeziestanley created

Hi guys, the IRepository always throws an exception if you try to get an entity with id when such id doesn't exist. But what I want is for it to return null. How can I do this. I am using Entity framework.


2 Answer(s)
  • User Avatar
    0
    chrisk created

    Hi,

    I believe that IRepository.Get() method is meant to return exception if record was not found. You can use .FirstOrDefault() to do that. For example UserManager class has GetById and FindById methods where later works as you want.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Chrisk ,

    Thanks for the answer :)