Last active
January 18, 2018 01:10
-
-
Save sbosell/029f09379428a68a27194d6155839df4 to your computer and use it in GitHub Desktop.
OrmLite Filter Transaction
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.Text; | |
using ServiceStack.OrmLite; | |
using ServiceStack.OrmLite.Sqlite; | |
using ServiceStack.DataAnnotations; | |
using System.Data; | |
using System; | |
var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider); | |
OrmLiteConfig.InsertFilter = (dbCmd, row) => { | |
IDbConnection db = dbCmd.Connection; | |
if (dbCmd.Transaction != null) { | |
db = dbCmd.Transaction.Connection; | |
} | |
try { | |
var modelDef = row.GetType().GetModelMetadata(); | |
//var name = modelDef.ModelName; | |
var dialectProvider = dbCmd.GetDialectProvider(); | |
var tableName = dialectProvider.NamingStrategy.GetTableName(modelDef); | |
var key = modelDef.GetPrimaryKey(row); | |
var auditLog = new Audit() { Id = 1 }; | |
var identity = db.Insert <Audit> (auditLog, selectIdentity: true); | |
} catch { | |
//ex.PrintDump(); | |
} | |
}; | |
var db = dbFactory.Open(); // Open ADO.NET DB Connection | |
public class Audit { | |
[AutoIncrement] | |
public long Id { | |
get; | |
set; | |
} | |
} | |
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 > (); | |
db.CreateTable < Audit > (); | |
var newTodo = new Todo { | |
Content = "Learn OrmLite", | |
Order = 1 | |
}; | |
using (var trans = db.OpenTransaction()) { | |
db.Save(newTodo); | |
} | |
var audits =db.Select<Audit>(); |
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="5.0.2" targetFramework="net45" /> | |
<package id="ServiceStack.Interfaces" version="5.0.2" targetFramework="net45" /> | |
<package id="ServiceStack.Common" version="5.0.2" targetFramework="net45" /> | |
<package id="ServiceStack.OrmLite" version="5.0.2" targetFramework="net45" /> | |
<package id="ServiceStack.OrmLite.Sqlite" version="5.0.2" targetFramework="net45" /> | |
</packages> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment