InvalidCast when creating new content item

Topics: Writing modules
Jul 23, 2013 at 9:30 PM
Edited Jul 23, 2013 at 9:30 PM
I'm creating a module to support podcasting, very similar to the blogs module where you have a podcast that contains multiple episodes. I'm modeling this module after the blogs module, but i'm having trouble coding the part when creating a new episode in the admin site.

The error occurs in my admin controller when I call this:
var podcastEpisode = Services.ContentManager.New<PodcastEpisodePart>("PodcastEpisode");
Picking through it, the exception happens here:
Orchard.ContentManagement.ContentCreateExtensions.New<T>(...)
Specifically when it calls contentItem.Get<T>()

The error is that the content item that was created does not contain my PodcastEpisodePart. I've been picking through the blogs module trying to see how this is getting added but I can't find it. In my migrations.cs, I can verify I have added the part when altering the type definition, just like the Blogs module, but somehow this part isn't being created in the content manager.

One difference is unlike the Blogs module, instead of how the BlogPost part isn't based on a record, my PodcastEpisodePart is based on my PodcastEpisodePart record as I need to store a few extra fields for each episode.

Any ideas as to what I'm missing?
Jul 28, 2013 at 8:13 PM
Make sure that the PodcastEpisode content type actually has the PodcastEpisodePart attached and that it is welded on. Welding happens automatically if you create a driver for your part, so be sure you have a driver for your part, e.g. PodcastEpisodePartDriver : ContentPartDriver<PodcastEpisodePart>.
Jul 29, 2013 at 1:08 AM
So I have the part driver created as well as attaching it to the content type in the Migrations.cs file. I've published the latest source to the CodePlex project (https://orchardpodcast.codeplex.com/) ... still haven't made progress on this one after a few days... banging my head against the wall.

I think it's something small I'm missing, but it still eludes me...
Jul 29, 2013 at 6:26 AM
I downloaded the module but I'm not getting the exception. Instead, it simply news up a content item of the requested type.
Jul 29, 2013 at 7:32 AM
Edited Jul 29, 2013 at 3:38 PM
When coding and changing migration content, sometimes the mappings.bin file (App_Data\Sites\Default when 1 site) which contains NHibernates mappings is not refreshed with your last changes and this could lead to errors like the one you encountered. Delete the file it will be rebuilt Ok. May be :).
Jul 29, 2013 at 8:25 PM
Strange as I too am not seeing the exception. I'm assuming it might have something to do with CSADNT's response, although I am pretty sure I cleared out the contents of App_Data\Sites\Default (I do it frequently to verify everything is working.

At any rate, looks like I'm working. I think my original problem stemmed from not having a driver yet for the part.

Thanks!