Look at tests/ServiceStack.OrmLite.Tests/Shared/UpdateCommandFilter.cs for a filter which does what you want and tests/ServiceStack.OrmLite.Tests/DefaultValueTests.cs for how to use it. (Sorry, I don’t know how to generate a github url for those.)
I don’t get values set when using the Default attribute and the variable method as shown.
I have a table:
CREATE TABLE TestTable
(
RecID UniqueIdentifier NOT NULL,
LastSavedDateTime DATETIME NOT NULL
)
A poco for that table:
public partial class TestTable
{
[Required]
[PrimaryKey]
public string RecID { get; set; }
[Required]
[Default("{GETDATE}")]
public DateTime LastSavedDateTime { get; set; }
}
And running this code yields an error
ServiceStack.OrmLite.SqlServer.SqlServer2016OrmLiteDialectProvider.Instance.Variables["{GETDATE}"] = "GETDATE()";
var dbFactory = new OrmLiteConnectionFactory("Data Source=localhost;Initial Catalog=TestDB;User ID=Dev;Password=*******", ServiceStack.OrmLite.SqlServer2016Dialect.Provider);
using (var db = dbFactory.Open())
{
TestTable testItem = new TestTable { RecID = System.Guid.NewGuid().ToString() };
db.Insert(testItem);
}
The error is:
System.Data.SqlClient.SqlException: 'Cannot insert the value NULL into column 'LastSavedDateTime', table 'TestDB.dbo.TestTable'; column does not allow nulls. INSERT fails.
The statement has been terminated.'
I’ve tried this on SS 5.0 from MyGet and also various different versions of the SQLDialect Providers.
What I really wanted was a subquery as the variable - e.g.:
ServiceStack.OrmLite.SqlServer.SqlServer2016OrmLiteDialectProvider.Instance.Variables["{NextSequenceNo}"] = "(SELECT COALESCE(MAX(SequenceNo), 0) + 1 FROM TestTable)";
I’ll have to resort to a custom insert, but it’d be nice if possible to have these variables working as I prefer the strong type philosophy.