Create a Database Class
Welcome to the first part of our Hello World tutorial!
We will start by creating a Starcounter application in Visual Studio by going to New Project -> Templates -> Visual C# -> Starcounter -> Starcounter Application
. We will name the application HelloWorld
.
Add a Persistent Class
Create a new class called Person
with the attribute [Database]
in the Program.cs
file. This attribute tag will make all instances of the class persistent.
Add the properties FirstName
and LastName
to this class. Your code should now look like this:
Add an Instance to the Class
Add a first instance to the class by defining a new person, its properties, and wrapping it in a Db.Transact()
. Using a transaction allows us to access database objects and makes the changes inside the transaction atomic and isolated.
The if statement here checks if you already have a Person
in the database by accessing the first result that we get from the query. If that is the case, you do not need to create a new one. Without it, we would create a new instance of Person
every time we run the program, which we do not intend to do.
Remember to import System.Linq
for FirstOrDefault
.
Result
Start your program with Starcounter by clicking F5 in Visual Studio. To see for yourself, open the administrator at localhost:8181/#/databases/default/sql
and enter SELECT * FROM HelloWorld.Person
. This will display all the instances, represented as rows, of the Person
class. Note that these instances are persistent. You can restart the application, or even the computer, and the instances will still be there.
For the next step, we'll add a UI which will help us to display the data in the browser.
If you get any errors, check your code against the source code.
Last updated