This happens to me only in production, not in local environments
can not set the 'Type' property (which is int) to a 'string' value
The specified cast from a materialized "System.Int32" type to the "System.String" type is not valid.
Here is part of the error
[InvalidOperationException: The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid.] System.Data.Entity.Core.CommonInternal.Materialization.ErrorHandlingValuereader 1.GetValue (DbDatareader reader, Int32 ordinal) +253 lambda_method (Closure, Shaper) +164 System.Data.Entity.Core.Common.Internal.Materalization.Coordinator 1.ReadNextElement (Shaper shaper) +197 System.Data.Entity.Core.Common.Internal.Materialization.SimpleEnumerator.MoveNext () +90 System.Data.Entity.Internal.LazyEnumerator 1.MoveNext () +108 System.Linq.WhereEnumerableIterator 1.MoveNext () +87 Def.CRM.Ventas.Presentacion.Controller.LoginController.SeleccionEmpresa (String idClient, String userID, String idSesion, Int16 cantHoras) +94
Usually we have to restart the IIS to work again
Function:
Function SeleccionEmpresa(ByVal idCliente As String, ByVal idUsuario As String, ByVal idSesion As String, Optional ByVal cantHoras As Int16 = 0) As ActionResult
Dim empresaMgr As New EmpresaMgr()
Dim empresas As IEnumerable(Of empresa) = empresaMgr.GetAllEmpresasCRM(idCliente)
Dim options As String = "<option value='0'>Seleccione su Empresa</option>"
For Each current As empresa In empresas
options += "<option value='" + current.IDEmpresa.ToUpper() + "'>" + current.Nombre + "</option>"
Next
Return View(New With {.idCliente = idCliente, .idUsuario = idUsuario, .idSesion = idSesion, .combo = options, .cantHoras = cantHoras})
End Function
Class:
Public Class EmpresaMgr
Private db As ERPEntities
Private idEmpresa As String
Private usaContextoExterno As Boolean = False
Public Sub New()
Me.db = New ERPEntities(AppConfiguracion.getConnectionString("ERPData", "Defontana"))
End Sub
Public Sub New(ByVal idEmpresa As String)
If String.IsNullOrWhiteSpace(idEmpresa) Then Throw New ArgumentException("idEmpresa")
Me.db = New ERPEntities(AppConfiguracion.getConnectionString("ERPData", "Defontana"))
Me.idEmpresa = idEmpresa
End Sub
Public Sub New(ByVal idEmpresa As String, ByRef context As ERPEntities)
If String.IsNullOrWhiteSpace(idEmpresa) Then Throw New ArgumentException("idEmpresa")
If context Is Nothing Then Throw New ArgumentNullException("context")
usaContextoExterno = True
Me.db = context
Me.idEmpresa = idEmpresa
End Sub
Public Function GetByKey() As empresa
Try
Dim empresa = (From c In db.empresas Where c.IDEmpresa.Equals(idEmpresa, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault()
Return empresa
Catch ex As Exception
Return Nothing
End Try
End Function
Public Function GetAll(ByVal idCliente As String) As IEnumerable(Of empresa)
Dim listaEmpresas As IEnumerable(Of empresa)
listaEmpresas = (From c In db.empresas Where c.IDCliente.Equals(idCliente, StringComparison.InvariantCultureIgnoreCase))
Return listaEmpresas.AsEnumerable()
End Function
Public Function GetAllEmpresasCRM(ByVal idCliente As String) As IEnumerable(Of empresa)
Dim listaEmpresas As IEnumerable(Of empresa)
listaEmpresas = GetAll(idCliente).Where(Function(f) f.IntCRM IsNot Nothing AndAlso f.IntCRM.Equals("S", StringComparison.InvariantCultureIgnoreCase))
Return listaEmpresas.AsEnumerable()
End Function
Public Function GetLogo() As byte()
Dim empresa = GetByKey()
If ((Not IsNothing(empresa)) AndAlso (Not IsNothing(empresa.ArcAdj))) Then
return empresa.ArcAdj
Else
Return Nothing
End If
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
If Not usaContextoExterno Then
db.Dispose()
db = Nothing
End If
End Sub
End Class