Search code examples
servicestackormlite-servicestack

How to use InsertOnly method in OrmLite?


Following this example, how is the correspondent for the method InsertOnly?

var updated = await dbCon.UpdateOnlyAsync(timesheet,
onlyFields: 
    x =>
        new
        {
            x.LogInTime,
            x.LogOffTime,
            x.IsFlaggedByLeader,
            x.LeaderComment,
            x.IsModified
        },
@where: x => x.Id == timesheet.Id) > 0; 

I couldn't find an example on Internet and in the Ormlite documentation.


Solution

  • You can find some examples in ApiSqlServerTests, e.g:

    db.InsertOnly(() => new Poco { FirstName = "Amy", Age = 27 });
    
    db.InsertOnly(new Poco { FirstName = "Amy", Age = 27 }, 
        p => new { p.FirstName, p.Age });
    
    db.InsertOnly(new Poco { Age = 27 }, p => p.Age);
    
    db.InsertOnly(new Poco { FirstName = "Amy", Age = 27 }, 
        new[] { "FirstName", "Age" });
    

    and async versions in ApiSqlServerTestsAsync, e.g:

    await db.InsertOnlyAsync(() => new Poco { FirstName = "Amy", Age = 27 });
    
    await db.InsertOnlyAsync(new Poco { FirstName = "Amy", Age = 27 }, 
        p => new { p.FirstName, p.Age });
    
    await db.InsertOnlyAsync(new Poco { Age = 27 }, p => p.Age);
    
    await db.InsertOnlyAsync(new Poco { FirstName = "Amy", Age = 27 }, 
        new[] { "FirstName", "Age" });