Migrating working ServiceStack to live causes Unable to cast object of type 'System.Byte' to type 'System.String'

StackOverflow https://stackoverflow.com/questions/21161339

Question

I have developed a ServiceStack API, using ORMLite based on a SQL Server. The app works perfectly pointing at both my local SQL database and an Azure database. Happy Days!

I have now tried to move this solution to the live server which has it's own local copy of the same database and I am getting strange results. The error is:

Error Code: InvalidCastException
Message: Unable to cast object of type 'System.Byte' to type 'System.String'.
[EMEM: 1/16/2014 11:49:29 AM]: [REQUEST: {Equipment:DP112}] 
System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.String'. at lambda_method(Closure , Object , Object ) at
ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.SetDbValue(FieldDefinition fieldDef, IDataReader dataReader, Int32 colIndex, Object instance) at 
ServiceStack.OrmLite.ReadExtensions.ExprConvertToList[T](IDataReader dataReader) at ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.ExprConvertToList[T](IDbCommand dbCmd, String sql) at 
ServiceStack.OrmLite.ReadConnectionExtensions.Exec[T](IDbConnection dbConn, Func`2 filter) at 
ViewPoint.EquipmentService.Get(EMEM request) at 
ServiceStack.Host.ServiceRunner`1.Execute(IRequest request, Object instance, TRequest requestDto)

I have checked the database schemas and they look identical.

This is the code that works on 2 out of the 3 databases quite happily but not the third.

public object Get(EMEM request)
    {
        var dbFactory = new OrmLiteConnectionFactory(WebConfigurationManager.ConnectionStrings["db"].ToString(), SqlServerDialect.Provider);
        using (IDbConnection db = dbFactory.OpenDbConnection())
        {
            if (request.Equipment == null)
            {
                List<EMEM> results = db.Select<EMEM>();
                return results;
            }
            else
            {
                List<EMEM> results = db.Select<EMEM>(p => p.Where(ev => ev.Equipment == request.Equipment));
                return results;
            }

        }
    }

I can literally fix the problem by pointing the connection string at the azure database which tends to suggest it's database related(?)

Extra Information:

  1. I have also written a put method which updates a row in the database and that works fine.
  2. On 2 of the servers EMEM is a table but on the third, where it doesn't work, it's a View.

Can anyone suggest where to start looking for this problem?

UPDATE: I have now created a View on my local development database so it should now be identical to the live database. I was expecting this to break the local dev site but it hasn't... :(

Was it helpful?

Solution

BINGO! FIXED IT!

IT WAS linked to the View, but it wasn't the View's fault....

The view was looking at a table with different data types against most of the values. The demo table I was working against had all the columns set to String!

So, look out when people give you "demo tables, with identical data to the live" to develop against.

They aren't always identical!!

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top