0

I am using Rob Conery's Massive.

The method List<DbCommand> BuildCommands(params object[] things), according to the methods comments, is supposed to take objects that "can be POCOs, Anonymous, NameValueCollections, or Expandos". But this:

var x = new { Id = new Guid("0F66CDCF-C219-4510-B81A-674CE126DD8C"), Name = "x", DisplayName = "y" };

myTable.BuildCommands(x);

Results in an InvalidCastException. Which reasonable since in the Massive.cs a cast from the passed in anonymous type to an ExpandoObject is attempted.

Why does the comment state you can pass in anything? Is there some other way to build commands from non-ExpandoObjects?

Here's some more code:

    public static void ThisFails()
    {
        DynamicModel myTable = new DynamicModel("myConnectionString", tableName: "dbo.MyTable", primaryKeyField: "Id");
        var updateMe = new { Id = new Guid("DF9A2F1B-3556-4EAC-BF2B-40E6821F3394"), Name = "abcx", DisplayName = "x" };
        var commands = myTable.BuildCommands(updateMe); // This fails
        myTable.Execute(commands);
    }

    public static void ThisSucceeds()
    {
        DynamicModel myTable = new DynamicModel("myConnectionString", tableName: "dbo.MyTable", primaryKeyField: "Id");
        dynamic updateMe = new ExpandoObject();
        updateMe.Id = new Guid("DF9A2F1B-3556-4EAC-BF2B-40E6821F3394");
        updateMe.Name = "abcx";
        updateMe.DisplayName = "x";
        var commands = myTable.BuildCommands(updateMe);
        myTable.Execute(commands);
    }

The code that fails results in:

Unable to cast object of type '<>f__AnonymousType03[System.Guid,System.String,System.String]' to type <br/> 'System.Collections.Generic.IDictionary2[System.String,System.Object]'.

It's thrown from the first line in your method

public virtual DbCommand CreateUpdateCommand(dynamic expando, object key)
{
     var settings = (IDictionary<string, object>)expando;
     ...

To me it looks like there should be a call to your extension method ToExpando before CreateUpdateCommand is called?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manolo
  • 1,597
  • 4
  • 21
  • 35
  • It's a bit difficult to understand what you're asking here. If this is an issue with Massive, it's a good idea to hit the issue list. If this is a general "why did I get this error" question - you'll need to add a bit more code with Exception details, etc. –  Sep 13 '12 at 17:47
  • Thanks for looking at this, added some more stuff to the post – Manolo Sep 14 '12 at 07:42
  • See also: https://github.com/robconery/massive/issues/144 – nemesv Sep 15 '12 at 05:24

1 Answers1

0

I think this is why people make methods private and public :). You're not supposed to call BuildCommands directly (though the code you have here still should work). I have a feeling there might be a bug that was committed in a patch.

That said - I believe this will work if you call myTable.Update() or myTable.Insert().

This last part answers the question - in terms of a possible "issue" - let's take that to Github.

  • I thought the idea was to call BuildCommands if you wanted to update/insert in a transaction? (That's why I'm not using Insert or Update) I was following the example at ["a-massive-update"](http://blog.wekeroad.com/helpy-stuff/a-massive-update) – Manolo Sep 17 '12 at 06:50