Skip to content

Instantly share code, notes, and snippets.

@mythz
Created November 4, 2016 12:11
Show Gist options
  • Save mythz/aa242a2bf9c6603ec391a86d9772e5eb to your computer and use it in GitHub Desktop.
Save mythz/aa242a2bf9c6603ec391a86d9772e5eb to your computer and use it in GitHub Desktop.
Insert Id in AutoIncrement PK at runtime
using ServiceStack;
using ServiceStack.Logging;
using ServiceStack.Text;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.Sqlite;
using ServiceStack.DataAnnotations;
LogManager.LogFactory = new ConsoleLogFactory();
var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
var db = dbFactory.Open(); // Open ADO.NET DB Connection
public class Todo
{
[AutoIncrement]
public long Id { get; set; }
public string Content { get; set; }
public int Order { get; set; }
public bool Done { get; set; }
}
db.CreateTable<Todo>();
var newTodo = new Todo {
Content = "Learn OrmLite",
Order = 1
};
db.Insert(newTodo);
typeof(Todo).GetModelMetadata().PrimaryKey.AutoIncrement = false;
db.Insert(new Todo {
Id = 10,
Content = "Inserted With Id",
Order = 1
});
db.Select<Todo>().PrintDump();
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ServiceStack.Text" version="4.5.0" targetFramework="net45" />
<package id="ServiceStack.Interfaces" version="4.5.0" targetFramework="net45" />
<package id="ServiceStack.Common" version="4.5.0" targetFramework="net45" />
<package id="ServiceStack.OrmLite" version="4.5.0" targetFramework="net45" />
<package id="ServiceStack.OrmLite.Sqlite.Mono" version="4.5.0" targetFramework="net45" />
</packages>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment