title: The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid when using stored procedure
body: <p>I have a stored procedure that looks like this:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[sp_SearchUnit]
      @UnitName VARCHAR(50)
  AS
  BEGIN
      SELECT
          b.NewBuildingId,
          b.NewBuildingName,
          u.PropertyUnitId,
          u.[Owner],
          p.FirstName,
          p.MiddleName,
          p.LastName,
          u.ParkingAreaSqMeters,
          u.PostingDateTime,
          u.TypeOfUnit,
          u.UnitName,
          u.WithParking
      FROM 
          [Property].[Unit_Rev] AS u
      LEFT JOIN
          [dbo].[Building_Rev] AS b ON u.BuildingId = b.NewBuildingId
      LEFT JOIN
          [dbo].[Person] AS p ON u.Owner = p.PersonId
      WHERE
          u.UnitName LIKE '%'+@UnitName+'%'
  END
  </code></pre>
  
  <p>And in my Web API, I access this stored procedure like this:</p>
  
  <pre><code>[HttpPost]
  [Route("api/searchunitbyunitname")]
  public async Task&lt;List&lt;SearchUnit&gt;&gt; SearchUnitByUnitName()
  {
      var req = HttpContext.Current.Request;
      var unitName = req["unitname"];
  
      const string query = "sp_SearchUnit @UnitName";
  
      using (var db = new PmisDbContext())
      {
          try
          {
              object[] parameters =
              {
                  new SqlParameter
                  {
                      ParameterName = "@UnitName",
                      Value = unitName,
                      Direction = ParameterDirection.Input
                  }
              };
  
              var list = await db.Database.SqlQuery&lt;SearchUnit&gt;(query, parameters).ToListAsync();
  
              return list;
          }
          catch(Exception ex)
          {
              return null;
          }
      }
  }
  </code></pre>
  
  <p>My class for 'SearchUnit' is:</p>
  
  <pre><code>public class SearchUnit
  {
          public string NewBuildingId { get; set; }
          public string NewBuildingName { get; set; }
          public string PropertyUnitId { get; set; }
          public string Owner { get; set; }
          public string FirstName { get; set; }
          public string MiddleName { get; set; }
          public string LastName { get; set; }
          public string ParkingAreaSqMeters { get; set; }
          public string PostingDateTime { get; set; }
          public string TypeOfUnit { get; set; }
          public string UnitName { get; set; }
          public string WithParking { get; set; }
  }
  </code></pre>
  
  <p>Why do I get this error: </p>
  
  <blockquote>
    <p>The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid</p>
  </blockquote>
  
  <p>If I run the stored procedure in SQL Server Management Studio, I get my results. </p>
  
  <p>Thank you for your help.</p>