How does OrmLite know about defaulted values with UpdateOnly

if I have a type like this:

class foo
    int primaryKey {get;set;}
    bool val1 {get;set;}
    bool val2 {get;set;}

and I update it like so:

Db.UpdateOnly(() => new foo() { val1 = true }, x=>x.primaryKey ==1234);

Is it because () to new foo() really an anonymous type? If it’s not, isn’t val2 going to set false?

In the statement Db.UpdateOnly(() => new foo() { val1 = true }, x=>x.primaryKey ==1234);, foo isn’t an anonymous type but the type is inferred by the return value of the Expression<Func<T>>.

public static int UpdateOnly<T>(this IDbConnection dbConn, 
            Expression<Func<T>> updateFields,
            Expression<Func<T, bool>> where = null,
            Action<IDbCommand> commandFilter = null)

The method (quote from comments) “Update only fields in the specified expression that matches the where condition (if any)”.

The expression gets converted to a SQL equivalent so val2 isn’t going to get expressed in the resultant SQL as it is not part of the expression even though it is part of the foo type you’ve declared.

In the above, I assumed your foo class looked more like

    class Foo
        public int PrimaryKey { get; set; }
        public bool Val1 { get; set; }
        public bool Val2 { get; set; }

Since if all the properties where private, the example UpdateDb code wouldn’t work.

A good way to experiment with what the SQL result for OrmLite is to use the Db.GetLastSql() method to log out what SQL was last executed.

Either that or a tool like PgHero if you are using PostgreSQL to track queries.

Yep or using a BeforeExecFilter like: