Just started playing with Servicestack and Ormlite (SqlServer). I was looking for a way to have tables auto created if they dont exist. I found :
CreateTableIfNotExists<T>
Which is cool, but is there anyway to either implement an interface that would be 'located' and auto created or a way to inject them somewhere to have them auto created?
It would be simple to implement something like that I just didnt want to reinvent the wheel if it was already there.
OrmLite is a code-first POCO ORM which doesn't require any base classes or interfaces so basically it's up to your application which POCO's should be used in OrmLite for creating RDBMS Tables with.
You could implement this yourself by looking for POCO's that have either an explicit [AutoIncrement]
or [PrimaryKey]
attribute. (Neither is strictly required as OrmLite also assumes the Id
property also defines the primary key).
Which would look like:
var tables = assembly.GetTypes()
.Where(x => x.GetProperties()
.Any(p => p.HasAttribute<AutoIncrementAttribute>() ||
p.HasAttribute<PrimaryKeyAttribute>()))
.ToArray();
db.CreateTableIfNotExists(tables);
Personally I wouldn't do this as I think it's important to be explicit and declarative about which tables should get created, it also serves as a reference point where all tables used in your App is listed allowing you to be able to easily navigate to the definitions of each of the tables with Ctrl + Click
(in R#).