none
Linq to SLQ & EntityRef done “cast not valid” RRS feed

  • Question

  • Hello!

    I have a strange problem with Linq to SLQ : Application thrown an exсeption "specified cast is not valid" on this action:

    valeur.ParametreX = (From par In db.Parametre
                         Where (par.Code_parametre = codeParametre)
                         Select par).Single()

    Where "valeur.ParametreX" is EntityRef:

    Private _ParametreX As EntityRef(Of Parametre)
    <Association(Storage:="_ParametreX", ThisKey:="Code_parametre")> _
    Public Property ParametreX() As Parametre
        Get
            Return _ParametreX.Entity
        End Get
        Set(ByVal value As Parametre)
            _ParametreX.Entity = value
            If Not IsNothing(value) Then
                _Code_parametre = _ParametreX.Entity.Code_parametre
            End If
        End Set
    End Property

    But if I go to another window with a lot of queries and return here - it works nice. I thought that this is a problem of lazy loading, but then I made uncomplicated test:

    valeur.ParametreX = (From par In db.Parametre
                         Where (par.Code_parametre = codeParametre And _
                                par.Unite_mesureX.Nom.Length < 0)
                         Select par).Single()

    I understood that in this line the nested table works, because it can calculate the length.

    Please tell me why it does not work immediately, but only after some queries in another form? Thanks!

    Tuesday, December 18, 2012 12:24 PM

Answers

  • Sorry, the topic can be deleted..
    It was very stupid mistake - DB was changet and no longer match the code (Int -> Byte)
    I just was misled by the fact that sometimes request succeeds..
    • Marked as answer by Alexander Sun Friday, December 21, 2012 6:41 AM
    Friday, December 21, 2012 12:15 AM

All replies

  • Hi,

    Please check whether the error occurs after delete the If clause:

    Private _ParametreX As EntityRef(Of Parametre)
    <Association(Storage:="_ParametreX", ThisKey:="Code_parametre")> _
    Public Property ParametreX() As Parametre
        Get
            Return _ParametreX.Entity
        End Get
        Set(ByVal value As Parametre)
            _ParametreX.Entity = value
        End Set
    End Property

    Thursday, December 20, 2012 7:16 AM
  • Sorry, the topic can be deleted..
    It was very stupid mistake - DB was changet and no longer match the code (Int -> Byte)
    I just was misled by the fact that sometimes request succeeds..
    • Marked as answer by Alexander Sun Friday, December 21, 2012 6:41 AM
    Friday, December 21, 2012 12:15 AM