-
-
Save glaidler/d738bd9d64af8adac57c343d6e1c5454 to your computer and use it in GitHub Desktop.
Insert Id in AutoIncrement PK at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 BaseTodo{ | |
public virtual long Id{get;set;} | |
} | |
public class Todo : BaseTodo | |
{ | |
[AutoIncrement] | |
[Alias("somalias")] | |
[PrimaryKey] | |
public override 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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