PetaPoco
PetaPoco copied to clipboard
Unable to Insert/Update Record using Expando Object in VB.NET
PetaPoco threw error message when inserting/updating records using expando object. It requires to manually create a class that represents the the row in order to insert it. This was hassle for developers to create a class which represents a row before inserting/updating records because it adds more time and lines of code
Can you use an anonymous type in your code? That avoids having to create a concrete class, and it's probably no more code than populating an expando.
Can you use an anonymous type in your code? That avoids having to create a concrete class, and it's probably no more code than populating an expando.
Do you have an idea on how to declare anonymous types in vb.net and how to use it on PetaPoco?
PetaPoco threw error message when inserting/updating records using expando object. It requires to manually create a class that represents the the row in order to insert it. This was hassle for developers to create a class which represents a row before inserting/updating records because it adds more time and lines of code
There's an example of inserting with an expando here: https://github.com/CollaboratingPlatypus/PetaPoco/wiki/Support-for-dynamic Does it throw an error if you follow this pattern?
@iadaz Inserts with dynamics appears to be broken at the moment. There is a pending PR which I think addresses this.
@rizalmart In C# the code would look something like this:
var newRecord = new {
field1 = "foo",
field2 = "bar"
};
db.Insert("my_table", newRecord);
I don't work in VB, but here are the relevant docs on anonymous types.
@iadaz Inserts with dynamics appears to be broken at the moment. There is a pending PR which I think addresses this.
@rizalmart In C# the code would look something like this:
var newRecord = new { field1 = "foo", field2 = "bar" }; db.Insert("my_table", newRecord);I don't work in VB, but here are the relevant docs on anonymous types.
I follow this method here from petapoco
`// Create a new record dynamic a = new ExpandoObject(); a.title = "My New Article";
// Insert it db.Insert("articles", "article_id", a);
// New record ID returned with a new property matching the primary key name Console.WriteLine($"New record {a.article_id} inserted");`
But it only threw error message that it does not allow expando object on insert
Yes, as I said, I think there's currently a bug. The code I gave above is for using anonymous types instead of Expandos. You can see that it's not much work, about the same as Expandos.
Hi,
I was facing a similar problem, but in C#, understand there is a bug but just wondering if there is a workaround.
So I was populating a ExpandoObject in a loop
IDictionary<string, object> row = new ExpandoObject();
for (int c = 0; c < colNames.Count; c++) {
row.Add(colNames[c], rowData[c]);
}
the column names of the table are in the colNames list and the data for the row is in rowData.
Then I insert this into the database using
db.Insert(tableName, "id", true, row)
but this throws an Exception
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Can I use the looping technique on an anonymous object? or is there any other way I get the data into the database from the 2 lists.