First, make sure you have already created a connection string in your web.config or app.config file.
1. First you download their Zip file here.
2. Extract the files and look for the directory T4 Templates >> Active Record.
3. Inside of the Active Record directory find the file "Settings.ttinclude".
4. Open it with a text editor and change line 21 to:
const string Namespace = "your desired namespace";
5 Set the ConnectionStringName equal to your connection string's id.
6. Set the DatabaseName constant to your database's name.
7. Save Settings.ttinclude.
8. Right click on your project and add a reference to SubSonic.Core.dll. It is found under the subsonic Bianaries folder.
9. Now this is the tricky part...I saw in their instructions that I needed to copy their files into my project and then in visual studio you are supposed to right click on their template files and then click on "Run custom tool". Unfortunately I couldn't see that option when I right clicked on the files. That is because you need to place them in a class library first. So you need to right click on your solution and add another project. Make it a class library project. Then go back and add a reference to it in your main website project.
10. Now copy all of the files from subsonic's T4 Templates folder into your new class library project. You also need to create an app.config file and add the connection string again to that file. You also need to add a reference to SubSonic.Core.dll.
11. Now right click on each one of the files and click "Run custom tool".
12. Now build your class project and build the solution, you may have to clean it before it will work properly.
13. Now if you add your namespace reference you should see a bunch of new variables. It looks like Subsonic 3 generates two classes for each table. One will be named the same as your table and the other one will be your table name with "Table" on the end.
Each time you make a change to your database you will need to run the custom tool for each file and then rebuild.
A simple select statement:
MyDatabase db = new MyDatabase();
SqlQuery simpleQuery = db.select.From <actualtablename>().Where(tablenameTable.idColumn).IsEqualTo(1);
List<actualtablename> blah = simpleQuery.ExecuteTypedList <actualtablename>()
You can also use linq chained methods like this: person.find(s=>s.person_id = 10);
To create a new person row you just enter:
person p = new person();
p.person_name = "bleh";
p.save();
No comments:
Post a Comment