Why does PetaPoco return from a db.Insert a ID field of 7 as a decimal

Jon picture Jon · Jun 7, 2011 · Viewed 8.2k times · Source

When doing a Insert the variable id is returned as an object. However in my database it is an int and in my POCO it is an int however when the method call to ExecuteScalar to return @@IDENTITY is called it returns the number 7 as an object but the debugger thinks its a decimal.

Therefore when I do int newID = (int)db.Insert(...) it throws a

InvalidCastException

Is this a framework bug or a PetaPoco bug?

Answer

Schotime picture Schotime · Jun 10, 2011

Also remember that if you have set your class up and you insert like so.

[TableName("Users")]
[PrimaryKey("Id")]
public class User {
    public int Id {get;set;}
    public string Name {get;set;}
}

var user = new User() { Name = "My Name" };
db.Insert(user);
Assert.True(user.Id != 0);

user.Id will now change from 0 to the newly created identity value