Getting Started (.NET 5)

  • 4 minutes to read

This tutorial demonstrates how to create an XPO-based .NET 5 console application, which initialize the data layer and perform basic data operations.

Tip

The complete .NET Core sample project is available at https://github.com/DevExpress/XPO/tree/master/Tutorials/Console.

You can find more XPO tutorials in the Getting Started with XPO topic.

Prerequisites

Install the .NET 5+ SDK and runtime.

Create a Project

Open the system console and execute the following command to create a new C# project (you can substitute MyXpoApp with your project name):

dotnet new console -o MyXpoApp

To create a VB.NET project, add the -lang vb switch to the command above.

Get XPO for .NET Core

Install the DevExpress.Xpo NuGet package.

cd MyXpoApp
dotnet add package DevExpress.Xpo

Install the Database Provider

Use the following command to install the Microsoft.Data.Sqlite package and use the local SQLite database:

dotnet add package Microsoft.Data.Sqlite

You can use any other .NET 5+ compatible provider XPO supports (see Database Systems Supported by XPO).

Define the Data Model

Edit the MyXpoApp/Program.cs(vb) file (or add a new code file to the project) and implement the following StatisticInfo persistent class with the Key, Info, and Date properties. The class is mapped to the StatisticInfo table with the Key, Info, and Date columns.

using DevExpress.Xpo;
// ...
public class StatisticInfo : XPLiteObject {
    public StatisticInfo(Session session)
        : base(session) {
    }
    Guid key;
    [Key(true)]
    public Guid Key {
        get { return key; }
        set { SetPropertyValue(nameof(Key), ref key, value); }
    }
    string info;
    [Size(255)]
    public string Info {
        get { return info; }
        set { SetPropertyValue(nameof(Info), ref info, value); }
    }
    DateTime date;
    public DateTime Date {
        get { return date; }
        set { SetPropertyValue(nameof(Date), ref date, value); }
    }
}

Setup the Data Layer

In the Program.cs(vb) file, change the Program.Main method. Pass the connection string to the XpoDefault.GetDataLayer method and assign the created IDataLayer object to the XpoDefault.DataLayer property.

using System.IO;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
// ...
class Program {
    static void Main(string[] args) {
        string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string connectionString = SQLiteConnectionProvider.GetConnectionString(Path.Combine(appDataPath, "myXpoApp.db"));
        XpoDefault.DataLayer = XpoDefault.GetDataLayer(connectionString, AutoCreateOption.DatabaseAndSchema);
    }
}

Make CRUD Operations With Data

Use the UnitOfWork API to execute create, read, update, and delete (CRUD) operations. For example, you can add the following code after the data layer initialization:

using System.Linq;
using DevExpress.Xpo;
// ...
// Create data:
Console.WriteLine($"Type some text to create a new 'StatisticInfo' record.");
string userInput = Console.ReadLine();
using (UnitOfWork uow = new UnitOfWork()) {
    StatisticInfo newInfo = new StatisticInfo(uow);
    newInfo.Info = userInput;
    newInfo.Date = DateTime.Now;
    uow.CommitChanges();

}
// Read data:
Console.WriteLine($"Your text is saved. The 'StatisticInfo' table now contains the following records:");
using (UnitOfWork uow = new UnitOfWork()) {
    var query = uow.Query<StatisticInfo>()
        .OrderBy(info => info.Date)
        .Select(info => $"[{info.Date}] {info.Info}");
    foreach (var line in query) {
        Console.WriteLine(line);
    }
}
// Delete data:
using (UnitOfWork uow = new UnitOfWork()) {
    var itemsToDelete = uow.Query<StatisticInfo>().ToList();
    Console.Write($"Records count is {itemsToDelete.Count}. Do you want to delete all records (y/N)?: ");
    if (Console.ReadLine().ToLowerInvariant() == "y") {
        uow.Delete(itemsToDelete);
        uow.CommitChanges();
        Console.WriteLine($"Done.");
    }
}

Save the changes in the Program.cs file and run the application. Use the dotnet run command in the system console to start the console application.

Note

Refer to the Deploy .NET Core Applications topic for information on how to deploy your application.