Created
January 23, 2018 12:32
-
-
Save mythz/bcc92336d4b567dcc290d1588b4043f5 to your computer and use it in GitHub Desktop.
OrmLite Dynamic Attribute Test
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 ServiceStack.Logging; | |
LogManager.LogFactory = new ConsoleLogFactory(debugEnabled:true); | |
var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider); | |
var db = dbFactory.Open(); // Open ADO.NET DB Connection | |
[Alias("DataItemTable")] | |
public class DataItem | |
{ | |
[PrimaryKey] | |
public int ItemKey { get; set; } | |
[StringLength(100)] | |
public string ItemDescription { get; set; } | |
} | |
public class DataItem2 | |
{ | |
public int ItemKey { get; set; } | |
public string ItemDescription { get; set; } | |
} | |
var type = typeof(DataItem2); | |
type.AddAttributes(new AliasAttribute("DataItem2Table")); | |
var prop = type.GetProperty(nameof(DataItem2.ItemKey)); | |
if (prop != null) | |
prop.AddAttributes(new PrimaryKeyAttribute()); | |
prop = type.GetProperty(nameof(DataItem2.ItemDescription)); | |
if (prop != null) | |
prop.AddAttributes(new StringLengthAttribute(100)); | |
using (var db = dbFactory.OpenDbConnection()) | |
{ | |
db.GetDialectProvider().GetStringConverter().UseUnicode = true; | |
db.CreateTableIfNotExists<DataItem>(); | |
db.CreateTableIfNotExists<DataItem2>(); | |
} |
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