locked
Linq gives 'Specific cast is not valid' when a WF is attached on a list RRS feed

  • Question

  • Hi,

    Something very strange.

    I have a publishing portal with a custom list. I used SPMetal to generate a class for a site. I use it to query a list:

     

     

     

     

     

     

    using (DemoDataContext contoso = new DemoDataContext(SPContext.Current.Web.Url))
    
    {
    
    EntityList<CtalkEditieItem> ctacEditie = contoso.GetList<CtalkEditieItem>("CtalkEditie");
    
    var query = from ctalk in ctacEditie.ToList()
    
    orderby ctalk.ArticleDate descending
    
    select ctalk;
    
    
    
    

     ...

    Works great. EXCEPT when I add a WF to this list. Either a ListWorkFlow or a SiteWorkflow that updates the list.

    I then get an exception:

    Specified cast is not valid.

    [InvalidCastException: Specified cast is not valid.] Set__updateMe(Object , Object ) +86 Microsoft.SharePoint.Linq.StaticPropertyMap.SetToEntity(Object entity, Object value) +71 Microsoft.SharePoint.Linq.SPItemMappingInfo.MaterializeEntity(DataContext dc, SPDataList list, SPListItem item, SPItemMappingInfo itemMappingInfo, JoinPath joinPath) +641 lambda_method(ExecutionScope , SPListItem ) +185 System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +302 MyProject.MailCtalk.MailCtalkUserControl.Page_Load(Object sender, EventArgs e) +964 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42 System.Web.UI.Control.OnLoad(EventArgs e) +132 System.Web.UI.Control.LoadRecursive() +66 System.Web.UI.Control.AddedControl(Control control, Int32 index) +350 MyProject.MailCtalk.MailCtalk.CreateChildControls() +155 System.Web.UI.Control.EnsureChildControls() +146 System.Web.UI.Control.PreRenderRecursiveInternal() +61 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394

     I've determined that the code failes on the the single line where a value for the WF exists. I also updated the SPMetal code so that it includes the WF:

    private System.Nullable<int> _updateMe;
    
    [Microsoft.SharePoint.Linq.ColumnAttribute(Name="UpdateMe", Storage="_updateMe", ReadOnly=true, FieldType="WorkflowStatus")]
    	public System.Nullable<int> UpdateMe {
    		get {
    			return this._updateMe;
    		}
    		set {
    			if ((value != this._updateMe)) {
    				this.OnPropertyChanging("UpdateMe", this._updateMe);
    				this._updateMe = value;
    				this.OnPropertyChanged("UpdateMe");
    			}
    		}
    	}
    

    I'm at a loss here. When I remove the WF everything works fine. It fails for every WF (OOB, Custom WF created with SD or VS2010)

    Any ideas?

    Friday, December 3, 2010 9:03 AM

Answers

  • Hi,

     

    Did this error still exist after modifying the SPMetal generated file?

    Would you please try regenerating the file after workflows attached?

    Hope this can help.

     

    Best Regards,

    Aaron

    • Marked as answer by sander_d Tuesday, December 14, 2010 8:34 AM
    Friday, December 10, 2010 5:32 AM

All replies

  • Hi,

     

    Did this error still exist after modifying the SPMetal generated file?

    Would you please try regenerating the file after workflows attached?

    Hope this can help.

     

    Best Regards,

    Aaron

    • Marked as answer by sander_d Tuesday, December 14, 2010 8:34 AM
    Friday, December 10, 2010 5:32 AM
  • Thnx for the reply.

    The problem stopped occuring...without me doin' something special. Hope it doesn't come back.

    PS: Yes I did regenerate the file.

    Thnx again!

    Tuesday, December 14, 2010 8:34 AM
  • Yes this problem still exists even after regenerating the LINQ(File generated by SPMetal) file. If I remove the workflow association LINQ works if I attach the workflow I get the error. If does not matter if I generate the LINQ file with or without the workflow associated.

     

    Thoughts?

    Tuesday, February 22, 2011 10:52 PM
  • Hi,

    yes, I know regenerating the file has no effect. Can you check if the associated column is visible in the list. If you remove it from the list the error will probably go away. $#% it's been a while since it occured. Don't know exactly what I did.

    Did you also 'Refresh' the SharePoint site in VS2010 Server Explorer and THEN regenerate the file?

    HTH

    Wednesday, February 23, 2011 6:41 AM
  • Hi @sander_d,
    I had the same problem, and this is what I did:
     
    1. Generate the code using SPMetal (with or without the Workflow attached: does not matter)
    2. Edit the generated code to change the type of the field that represents the workflow status column from the list, to object. In my case, the changed code looks like:
     
      //this is the most important change: the type of the field is now *object*
    [System.Runtime.Serialization.DataMemberAttribute()]
      private object _designCompany_SetCompanyAndFullName;
    
      [Microsoft.SharePoint.Linq.ColumnAttribute(Name = "SetCompa", Storage = "_designCompany_SetCompanyAndFullName", ReadOnly = true, FieldType = "WorkflowStatus")]
      public System.Nullable<int> DesignCompany_SetCompanyAndFullName
      {
       get
       {
        return null; //removed all code - but this may be unnecessary
       }
       set
       {
        //removed all code - but this may be unnecessary
       }
      }
    
    
    3. Compile and deploy.
    The error 'specified cast..' should not occur anymore.
    The reason for this is that (I believe) SPMetal has a bug and generates System.Nullable<int> type fields in the generated code to represent workflow status columns. I checked out the code for Microsoft.Sharepoint.Linq.dll in Reflector, to find that it uses a dynamically generated method to set the field value directly and just before setting the field it tries to cast the value from object to whatever the field type is.
    MSDN specifies that the type mapping for the 'WorkflowStatus' type in Sharepoint is 'object' in .NET : http://msdn.microsoft.com/en-us/library/ee536245.aspx
    Now, atleast  I don't get this error anymore.
    - Krishna

    Krishna
    • Proposed as answer by Krishna Nadiminti Tuesday, May 17, 2011 5:28 AM
    • Edited by Krishna Nadiminti Tuesday, May 17, 2011 6:04 AM Emphasized the actual change that was made.
    Tuesday, May 17, 2011 5:28 AM
  • Thanks your solution worked. I've actually deleted workflow fields and it works fine.
    Tuesday, May 24, 2011 8:24 AM
  • This is a bug. I wonder how much less maintainable my code just got with the fact that there's now a giant wonky wonk in it.

    Wednesday, May 25, 2011 6:13 PM
  • Cheers Krishna  your info helped me.
    Drasko Popovic
    Friday, August 26, 2011 1:24 PM
  • Had the same issue, didn't like editing the generated context manually.

     

    Using the SPMetal XML parameter, you can use the ExcludeColumn to exclude the workflow fields. You can find these fields in the generated context by looking for "WorkflowStatus".

    Example for the XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <Web xmlns="http://schemas.microsoft.com/SharePoint/2009/spmetal">
      <List Name="Animals">
        <ContentType Name="Item" Class="AnimalsItem">
          <!-- Excluding evil workflow fields -->
          <ExcludeColumn Name="AnimalsAd" />
          <ExcludeColumn Name="AnimalsUp" />
        </ContentType>
      </List>
      <List Name="Plants">
        <ContentType Name="Item" Class="PlantsItem">
          <!-- Excluding evil workflow fields -->
          <ExcludeColumn Name="Plants" />
          <ExcludeColumn Name="Plants0" />
        </ContentType>
      </List>
    </Web>

    Thursday, September 22, 2011 1:56 PM
  • This worked for me. Thanks Krishna this has been driving me nuts for a week now. How did you figure it out?
    Friday, February 10, 2012 11:05 PM
  • Thanks yellowblood, this was the right solution for me!
    Thursday, May 31, 2012 8:10 AM