Search code examples
c#castle-activerecord

TryFindByPrimaryKey in Castle ActiveRecord


How do I search for a record by primary key, but return null if it doesn't exist?

public static T FindByPrimaryKeyOrDefault(object id)
{
  try
  {
    return ActiveRecordMediator<T>.FindByPrimaryKey(id);
  }
  catch (NotFoundException)
  {
    return null;
  }
}

Solution

  • ActiveRecordMediator<T>.FindByPrimaryKey(id, false);
    

    I'll copy questions like these to the ActiveRecord wiki.