Skip to content

Instantly share code, notes, and snippets.

@mythz
Created January 23, 2018 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mythz/bcc92336d4b567dcc290d1588b4043f5 to your computer and use it in GitHub Desktop.
Save mythz/bcc92336d4b567dcc290d1588b4043f5 to your computer and use it in GitHub Desktop.
OrmLite Dynamic Attribute Test
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>();
}
<?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