using Starcounter.Nova.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace StarcounterConsoleSample
public abstract class Person
public abstract string Name { get; set; }
public static void Main()
string connectionString =
"Database=./.database/StarcounterConsoleSample;"
+ "OpenMode=CreateIfNotExists;"
+ "StartMode=StartIfNotRunning;"
+ "StopMode=IfWeStarted";
// Here we create a service collection that
// we add the Starcounter services to.
// When we call BuildServiceProvider(), we get an instance
// that we can use to fetch service instances,
// for example ITransactor, which we then can use to
// to make database transactions.
using var services = new ServiceCollection()
.AddStarcounter(connectionString)
// Here we fetch our ITransactor instance from the service provider.
var transactor = services.GetRequiredService<ITransactor>();
// And here we use it to make a database transaction.
var name = transactor.Transact(db =>
// Here inside the transaction,
// we can use the IDatabaseContext instance
// to interact with the Starcounter database.
// We can query it using SQL
// (which returns an IEnumerable<Person>
// that we can use with LINQ).
"SELECT p FROM Person p WHERE Name = ?",
// We can insert new rows in the database using Insert().
// We write to the database row
// using standard C# property accessors.
// Let's return the name as result of the transaction.
// And let's print it in the console