using Microsoft.Extensions.DependencyInjection;
using Starcounter.Nova.Hosting;
/// Here is our database table. It contains people. With names.
public abstract class Person
public abstract string Name { get; set; }
/// This simple console application demonstrates how to build a service provider
/// for the Starcounter services, to fetch service instances from it,
/// and then how to use the services to make basic database transactions
/// with basic database interactions like SQL queries and inserts.
public static void Main(string[] args)
string name = args.FirstOrDefault() ?? "Noname";
string connectionString = "Database=./.database/ConsoleApp;"
+ "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.
ulong oid = 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 App.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 object id as result of the transaction.
// And let's print it in the console.
Console.WriteLine(quot;{oid}: {name}");