CreateUpdateCommand & CreateInsertCommand don't work with POCOs or anonymous types #144

nlotter opened this Issue Jun 11, 2012 · 4 comments


None yet
3 participants

nlotter commented Jun 11, 2012

Absolutely love this - thanks for putting it out there!

Try something like this:
MyTable.Save(new {Id=1,Name="Bob"});

Result is a RunTimeBinderException - unable to cast to 'System.Collections.Generic.IDictionary<string,object>' - which makes sense because the object passed to CreateUpdateCommand isn't an ExpandoObject & doesn't implement IDictionary<string,object>.

Might be missing something obvious or screwing with some feature I'm not aware of but... this works for me:

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

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

(do same for CreateInsertCommand)

I can confirm the same behavior here but it looks like you are trying to use Save to Insert. Have you tried MyTable.Insert(new {Id=1,Name="Bob"});? This works for me. Although I omit the 'Id' because the field is an identity in my database.

nlotter commented Jun 11, 2012

You're right - insert works fine. I can see why:

Public virtual dynamic Insert(object o) {            
var ex = o.ToExpando();

The thing to be inserted is passed in as object (not dynamic) and then explicitly converted to ExpandoObject before CreateInsertCommand is called.

I think my tiny example was a bit contrived :) (I'm calling save because I want to insert / update multiple items in a transaction)

This may seem a bit naive (I'm new to using C#'s dynamic feature) but are we losing anything by changing CreateUpdateCommand & CreateInsertCommand to take an object instead of dynamic? In lieu of Rob stopping in to chime in on this, do you think this is the right fix?


robconery commented Jun 20, 2012

Best to use Insert/Update as needed. If you guys would like to discuss further I invite you to move the convo to twitter or SO perhaps.

@robconery robconery closed this Jun 20, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment