I need to use OrmLite for SQL Server in a new Visual Studio C# console application using a database-first approach. I have some questions about the POCO generation process.
How can I exclude certain schemas from being converted into POCOs? For example, I want to completely ignore the "dbo" schema.
I have two tables named "Tracking" in different schemas in my database. When I exclude the tables in the .tt file like so:
tables["Tracking"].Ignore = true;
I get an error when running the .tt file that says the exclude statement matches multiple tables. How can I exclude these tables?
I would like to use aliases for table and column names. This page shows how to do that. From this example, the .tt file will not be able to give the aliases I need because aliases are given on an individual table and column basis. Do I need to copy the POCOs generated from the .tt file into individual files somewhere else in the project and then give those POCOs the aliases I need? How do aliases work in OrmLite with a database-first approach?
To answer your first 2 questions - If you look at the beginning of the .tt file, you can see how the code loops through the Table objects to create the POCO classes. In this line you can see the beginning of the loop:
foreach(Table tbl in from t in tables where !t.Ignore select t)
You can see that it doesn't loop through tables with the t.Ignore property is set to "true". You can just add to the where clause of that linq statement to ignore an entire schema, like so:
foreach(Table tbl in from t in tables where !t.Ignore && t.Schema != "dbo" select t)
Now that you understand that section of code you can add any filtering you need to the where clause, such as:
foreach(Table tbl in from t in tables where !t.Ignore && t.Schema != "dbo" && t.Name != "Tracking" select t)
This would ignore any table named "Tracking", regardless of what its "Schema" property value was. It probably would be better form to break out the IEnumerable ("from t in tables ...") into a separate variable so you could make it as complex as you need it to be and keep your code readable. Adding a bunch of additional "where" clauses inside the "foreach" would get ugly fast.