2010-03-12 4 views
5

Je suis en train de faire ce qui suit ...LINQ to SQL - Le cast spécifié n'est pas valide - SingleOrDefault()

Request request = (
    from r in db.Requests 
    where r.Status == "Processing" && r.Locked == false 
    select r 
).SingleOrDefault(); 

Il jette l'exception suivante ...

Message: 
Specified cast is not valid. 

StackTrace: 
    at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) 
    at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) 
    at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) 
    at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression) 
    at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source) 
    at GDRequestProcessor.Worker.GetNextRequest() 

Quelqu'un peut-il m'aider? Merci d'avance!


détails du schéma se trouvent ci-dessous ...

[Table(Name="dbo.Requests")] 
public partial class Request : INotifyPropertyChanging, INotifyPropertyChanged 
{ 

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); 

    private int _RequestId; 

    private string _LoanNumber; 

    private string _ClientCode; 

    private int _RequestTypeId; 

    private bool _HasParameters; 

    private string _Status; 

    private bool _Locked; 

    private string _ErrorMessage; 

    private int _ReferenceId; 

    private EntitySet<RequestParameter> _RequestParameters; 

    private EntityRef<RequestType> _RequestType; 

#region Extensibility Method Definitions 
partial void OnLoaded(); 
partial void OnValidate(System.Data.Linq.ChangeAction action); 
partial void OnCreated(); 
partial void OnRequestIdChanging(int value); 
partial void OnRequestIdChanged(); 
partial void OnLoanNumberChanging(string value); 
partial void OnLoanNumberChanged(); 
partial void OnClientCodeChanging(string value); 
partial void OnClientCodeChanged(); 
partial void OnRequestTypeIdChanging(int value); 
partial void OnRequestTypeIdChanged(); 
partial void OnHasParametersChanging(bool value); 
partial void OnHasParametersChanged(); 
partial void OnStatusChanging(string value); 
partial void OnStatusChanged(); 
partial void OnLockedChanging(bool value); 
partial void OnLockedChanged(); 
partial void OnErrorMessageChanging(string value); 
partial void OnErrorMessageChanged(); 
partial void OnReferenceIdChanging(int value); 
partial void OnReferenceIdChanged(); 
#endregion 

    public Request() 
    { 
     this._RequestParameters = new EntitySet<RequestParameter>(new Action<RequestParameter>(this.attach_RequestParameters), new Action<RequestParameter>(this.detach_RequestParameters)); 
     this._RequestType = default(EntityRef<RequestType>); 
     OnCreated(); 
    } 

    [Column(Storage="_RequestId", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] 
    public int RequestId 
    { 
     get 
     { 
      return this._RequestId; 
     } 
     set 
     { 
      if ((this._RequestId != value)) 
      { 
       this.OnRequestIdChanging(value); 
       this.SendPropertyChanging(); 
       this._RequestId = value; 
       this.SendPropertyChanged("RequestId"); 
       this.OnRequestIdChanged(); 
      } 
     } 
    } 

    [Column(Storage="_LoanNumber", DbType="VarChar(50) NOT NULL", CanBeNull=false)] 
    public string LoanNumber 
    { 
     get 
     { 
      return this._LoanNumber; 
     } 
     set 
     { 
      if ((this._LoanNumber != value)) 
      { 
       this.OnLoanNumberChanging(value); 
       this.SendPropertyChanging(); 
       this._LoanNumber = value; 
       this.SendPropertyChanged("LoanNumber"); 
       this.OnLoanNumberChanged(); 
      } 
     } 
    } 

    [Column(Storage="_ClientCode", DbType="VarChar(50) NOT NULL", CanBeNull=false)] 
    public string ClientCode 
    { 
     get 
     { 
      return this._ClientCode; 
     } 
     set 
     { 
      if ((this._ClientCode != value)) 
      { 
       this.OnClientCodeChanging(value); 
       this.SendPropertyChanging(); 
       this._ClientCode = value; 
       this.SendPropertyChanged("ClientCode"); 
       this.OnClientCodeChanged(); 
      } 
     } 
    } 

    [Column(Storage="_RequestTypeId", DbType="Int NOT NULL")] 
    public int RequestTypeId 
    { 
     get 
     { 
      return this._RequestTypeId; 
     } 
     set 
     { 
      if ((this._RequestTypeId != value)) 
      { 
       if (this._RequestType.HasLoadedOrAssignedValue) 
       { 
        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); 
       } 
       this.OnRequestTypeIdChanging(value); 
       this.SendPropertyChanging(); 
       this._RequestTypeId = value; 
       this.SendPropertyChanged("RequestTypeId"); 
       this.OnRequestTypeIdChanged(); 
      } 
     } 
    } 

    [Column(Storage="_HasParameters", DbType="Bit NOT NULL")] 
    public bool HasParameters 
    { 
     get 
     { 
      return this._HasParameters; 
     } 
     set 
     { 
      if ((this._HasParameters != value)) 
      { 
       this.OnHasParametersChanging(value); 
       this.SendPropertyChanging(); 
       this._HasParameters = value; 
       this.SendPropertyChanged("HasParameters"); 
       this.OnHasParametersChanged(); 
      } 
     } 
    } 

    [Column(Storage="_Status", DbType="VarChar(50) NOT NULL", CanBeNull=false)] 
    public string Status 
    { 
     get 
     { 
      return this._Status; 
     } 
     set 
     { 
      if ((this._Status != value)) 
      { 
       this.OnStatusChanging(value); 
       this.SendPropertyChanging(); 
       this._Status = value; 
       this.SendPropertyChanged("Status"); 
       this.OnStatusChanged(); 
      } 
     } 
    } 

    [Column(Storage="_Locked", DbType="Bit NOT NULL")] 
    public bool Locked 
    { 
     get 
     { 
      return this._Locked; 
     } 
     set 
     { 
      if ((this._Locked != value)) 
      { 
       this.OnLockedChanging(value); 
       this.SendPropertyChanging(); 
       this._Locked = value; 
       this.SendPropertyChanged("Locked"); 
       this.OnLockedChanged(); 
      } 
     } 
    } 

    [Column(Storage="_ErrorMessage", DbType="VarChar(255)")] 
    public string ErrorMessage 
    { 
     get 
     { 
      return this._ErrorMessage; 
     } 
     set 
     { 
      if ((this._ErrorMessage != value)) 
      { 
       this.OnErrorMessageChanging(value); 
       this.SendPropertyChanging(); 
       this._ErrorMessage = value; 
       this.SendPropertyChanged("ErrorMessage"); 
       this.OnErrorMessageChanged(); 
      } 
     } 
    } 

    [Column(Storage="_ReferenceId", DbType="Int NOT NULL")] 
    public int ReferenceId 
    { 
     get 
     { 
      return this._ReferenceId; 
     } 
     set 
     { 
      if ((this._ReferenceId != value)) 
      { 
       this.OnReferenceIdChanging(value); 
       this.SendPropertyChanging(); 
       this._ReferenceId = value; 
       this.SendPropertyChanged("ReferenceId"); 
       this.OnReferenceIdChanged(); 
      } 
     } 
    } 
+1

Cela se produit généralement lorsque le type d'une ou de plusieurs propriétés de l'objet C# ne correspond pas au type de la colonne. pouvez-vous publier le schéma pour la table Request et les types pour les propriétés de la classe Request? –

+0

Quel est le type de demande? Que faire si vous utilisez une requête var? Quel est le type de retour de SingleOrDefault? –

+0

La demande reflète un enregistrement dans la table de base de données Requêtes ... J'ai essayé d'utiliser le code ci-dessous et ai toujours l'erreur ... var query = (de r dans db.Request où r.Status.Equals ("Processing") && r.Locked.Equals (false) sélectionnez r) .SingleOrDefault(); le type de retour de SingleOrDefault est la demande – NullReference

Répondre

0

vous devriez écrire: .SingleOrDefault<Request>().

Request request = (
         from r in db.Requests 
         where r.Status == "Processing" && r.Locked == false 
         select r 
       ) 
        .SingleOrDefault<Request>(); 
9

Vous devez vous assurer que votre schéma de table correspond à votre diagramme .dbml et aux propriétés associées.

La façon la plus simple de le faire est peut-être de supprimer la table en question de votre diagramme dbml. Ensuite, à partir de l'explorateur de votre serveur, faites-le glisser vers le diagramme.

Remarque: Cela ne fonctionnera pas toujours avec les vues. Parfois, vous devez exécuter exec sp_refreshview MyViewName à partir de SSMS avant de rajouter votre vue à DBML.

+1

Cette approche a fonctionné pour moi, merci - sauvé beaucoup de temps. Une fois que j'ai supprimé la nouvelle table dans le DBML correspondant à ma base de données actuelle, j'ai comparé l'entrée de la table dans le fichier * .dbml avec la nouvelle pour trouver les différences. Atom avec le paquet Split Diff m'a donné les détails exacts. – Michael12345

0

Supprimez simplement la table appropriée de votre fichier dbml et ajoutez-la à nouveau. Si cela ne fonctionnait pas, essayez de supprimer les associations du fichier dbml.