Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
nlotter opened this Issue · 4 comments

3 participants

@nlotter

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' - which makes sense because the object passed to CreateUpdateCommand isn't an ExpandoObject & doesn't implement IDictionary.

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)expando;

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

(do same for CreateInsertCommand)

@frankhale

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

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)

@frankhale

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
Collaborator

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.